Repository navigation
Description
I'm submitting a ... (check one with "x")
[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Currently NgModel does object reference comparison. This is not very flexible.
Minimal reproduction of the problem with instructions
See example with two multi selects - first one doesn't use workaround to enforce usage of same instances. The second one does.
What is the motivation / use case for changing the behavior?
Objects do not always come from the same source. They typically come from some REST service. And they usually don't have the same instance.
Current behavior requires workaround for something I believe to be very (if not the most) common use case. I think those should be supported by framework without requiring any workarounds.
Suggested solution
I suggest creating comparator object (or callback) which would compare two objects and return true if they are equal. NgModel would then internally use this comparator to compare objects.
Resulting code could look like this:
<select multiple class="form-control"
name="selectedCountries"
[ngComparator]="countryComparator"
[(ngModel)]="selectedCountries">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>Framework could also provide some default comparators - for example one, which would compare object attributes. That one would be sufficient for quite a lot of use cases.
If there already is some standard solution, please point me to it. I tried to search and I didn't find anything. I found just this stack overflow post describing the same problem, but not providing any solution.
Activity
noamichael commentedon Jan 12, 2017
Would love to see this implemented, because I have many places in my application where dropdown values are loaded from a rest service dynamically, and they are represented by complex objects. Without a way to track them, I am forced to instead force singleton instances on the client-side, or write a custom input for the object which assigns the value of the
selectto be a primitive and then later search for the proper instance.noamichael commentedon Jan 12, 2017
As a temporary solution, I've written the following component, which allows the user to pass in a label for each option, and a field to track the options by:
and can be used like this
const accountTypes = [ {id: 1, name: "User"}, {id: 2, name: "Admin"} ]...
<trackable-select formControlName="mySelect" [options]="accountTypes" emptyLabel="Select Account Type" label="name" trackBy="id"> </trackable-select>fix(forms): provide a method to compare options (#13349)
fix(forms): provide a method to compare options (angular#13349)
fix(forms): provide a method to compare options (angular#13349)
oskarols commentedon Jan 15, 2018
Was there any reason why this was not applicable to radio inputs?
angular-automatic-lock-bot commentedon Sep 13, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.