You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
<ion-select> cannot select correct option if its values are objects, and ngModel is tied to a different object. #6625
Suggestions:
The option to select is determined by a function that checks for exact equality (or object equality). Perhaps there should be an option that the user can pass in to the ion-select, something like trackBy, that will examine the object values' properties instead of the objects themselves.
I've implemented a solution, and it seems to work properly, and I plan to submit a PR.
I've added an @input() property that takes in a string value that represents the object property to compare. My only question is, what should I name it? As of now, it is called 'trackBy'. I could also call it 'property' or something like that. Any feedback?
I've already commented the below on the PR for this issue. I'm putting it here just for an update and a work around solution.
@cleever, angular does support this but not for my specific use case. I am pulling my pre-selected objects from a database. Therefore, the list of objects in my component, and the actual object used for the model value is a completely different object, even though it has the exact same properties.
I ended up doing this, which is similar to @xr0master's solution.
<ion-select[ngModel]="someProperty.id" (ngModelChange)="selectObjectById(list, $event, 'someProperty')"><ion-option*ngFor="let item of list" [value]="item.id">{{item.name}}</ion-option></ion-select>
Hey ionic team @jgw96@manucorporat ! This could be fixed by using Angular 4's new compareWith directive. Select API and compareWith
Those docs also explain well the problem that I have been trying to express this whole time.
@granthoff1107 You can try compareWith directive to solve this problem. It works well. But I still didn't find a way to correctly use ion-select with the data from a promise.
Reacted by bkarv, Mateo Calle, santoshtamhane, Chris O'Brien, Kevin Barrientos, Ahmed Nabil, Nickson Yap, Kesavamoorthi Subramanian, jwburnside, Eric Mulcahy and 1 more
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.
Activity
zakton5 commentedon May 23, 2016
I've implemented a solution, and it seems to work properly, and I plan to submit a PR.
I've added an @input() property that takes in a string value that represents the object property to compare. My only question is, what should I name it? As of now, it is called 'trackBy'. I could also call it 'property' or something like that. Any feedback?
zakton5 commentedon Aug 4, 2016
I've already commented the below on the PR for this issue. I'm putting it here just for an update and a work around solution.
@cleever, angular does support this but not for my specific use case. I am pulling my pre-selected objects from a database. Therefore, the list of objects in my component, and the actual object used for the model value is a completely different object, even though it has the exact same properties.
I ended up doing this, which is similar to @xr0master's solution.
This is essentially what the pull request did in the first place. It's unfortunate that this is necessary, but it works as desired.
jonathan-chin commentedon Oct 11, 2016
I am currently experiencing the same issue. ion-select does not behave as expected when value is an object.
any updates on this?
rolandjitsu commentedon Dec 15, 2016
The proper option does not get selected neither if the value is
null:If
categoryisnullI'd expect that the first option withNoneis selected, but neither options is selected.dpanko2005 commentedon Mar 14, 2017
This worked for me. The selected value is an object
<ion-item> <ion-label>*Select Units : </ion-label> <ion-select (ionChange)="unitSelectionChangeModal()" class="gsValidate gsSmallText" name="activeUnit" [(ngModel)]="activeUnit" required> <ion-option value=""></ion-option> <ion-option *ngFor="let item of activeSupportedUnits" [value]="item">{{item.abbrev}}</ion-option> </ion-select> </ion-item>zakton5 commentedon Jun 3, 2017
Hey ionic team @jgw96 @manucorporat ! This could be fixed by using Angular 4's new
compareWithdirective. Select API and compareWithThose docs also explain well the problem that I have been trying to express this whole time.
zakton5 commentedon Jun 7, 2017
I'm just going to make a PR for this and use the same logic as Angular's
compareWithyanxiaodi commentedon Jul 11, 2017
I found the same problem. And if the data comes from a promise, it still has error about ngModel binding.
granthoff1107 commentedon Jul 22, 2017
Why is it always impossible to get a response from the Ionic Team?
yanxiaodi commentedon Jul 23, 2017
@granthoff1107 You can try
compareWithdirective to solve this problem. It works well. But I still didn't find a way to correctly useion-selectwith the data from a promise.pk359 commentedon Oct 5, 2017
<ion-select #country [(ngModel)]="countryData" > <ion-option *ngFor="let country of countries" value="{{stringify(country)}}" {{country.name}}</ion-option> </ion-select>please note stringfiy is a function that just stringifies whole object. It may pose performance issue.
chrisjpalmer commentedon Oct 10, 2017
This works for me.
Following the documentation linked above:

I made the following in component html:

And made the following function for comparing the different objects:

My ionic info is:
bkarv commentedon Oct 13, 2017
The above worked for me too. Thanks!
MattWlodarski commentedon May 1, 2018
The above worked for me too even if I load in the options with an async pipe.
NicksonYap commentedon May 4, 2018
The above worked for me too! Angular/Ionic should have this built in.
Probably a [trackBy]="country.id" if using this example #6625 (comment)
ionitron-bot commentedon Sep 1, 2018
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.