Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compareWith does not work with bindValue #535

Closed
tomasz-nowakowski opened this issue May 8, 2018 · 4 comments
Closed

compareWith does not work with bindValue #535

tomasz-nowakowski opened this issue May 8, 2018 · 4 comments
Assignees
Labels

Comments

@tomasz-nowakowski
Copy link

Expected behavior

When I use both compareWith and bindValue I would expect that compareWith will still used for comparing.

Actual behavior

Run this with console opened.
https://stackblitz.com/edit/angular-d1cvpv?file=src/app/hello.component.ts
Nothing shows up in console. But if you remove bindValue the "test" from compareFn should appear in console two times.

Why would I need this

Because my initial ngModel is always a string but values in items might be strings or numbers. I want to convert ngModel and values to sting before comparing them in compareWith

More Info

ng-select version: 1.4.1

browser: all

reproducible in demo page: NO

@varnastadeus
Copy link
Member

varnastadeus commented May 10, 2018

@tomasz-nowakowski-stx I am not sure if it makes sense to use compareWith with bindValue. Here is why:

  1. When you use let say bindValue="id", you already tell that your value should match id property of items list so having compareWith seems redundant.

  2. It will produce inconsistent results.

items = [{ id: 7, name: 'Vilnius' }]

Eg.: if you have your value as string '7', initial NgModel will stay as string, but if you select it from the dropdown, NgModel would switch to number.

Having this in mind, solution for your case would be casting string to number before setting the model.

@tomasz-nowakowski
Copy link
Author

@varnastadeus The problem is that I never know if values from API are strings or numbers (don't blame me 😃). And inconsistency you mentioned is fine for me in this case.
But I see your arguments and that it might be confusing for some people in the future. But compareWith working only in some cases might me confusing also as it is was for me :)

@rkalita
Copy link

rkalita commented May 29, 2018

Hi guys! I'm the one of developers who, as @varnastadeus said, faced with the same problem. ng-select is really good plugin, but if someone wants to embed it into big project it leads to big problems because of strict comparison('==='). I don't ask you guys to change just compareWith behavior, but maybe you can help by the other way!? As a result for now you're losing a big percent of developers that want to use your product because as I said implementing ng-select into big project takes a lot of time and necessity to write some additional middleware because of ng-select that's not good in my opinion

@anjmao
Copy link
Member

anjmao commented May 29, 2018

#586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants