-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
<ion-select> cannot select correct option if its values are objects, and ngModel is tied to a different object. #6625
Comments
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> public selectObjectById(list: any[], id: string, property: string) {
var item = list.find(item => item._id === id);
var prop = eval('this.' + property);
prop = property;
} This is essentially what the pull request did in the first place. It's unfortunate that this is necessary, but it works as desired. |
I am currently experiencing the same issue. ion-select does not behave as expected when value is an object. any updates on this? |
The proper option does not get selected neither if the value is <ion-select [ngModel]="category" (ngModelChange)="selectCategory($event)" name="category">
<ion-option [value]="null">
None
</ion-option>
<ion-option *ngFor="let category of categories | async" [value]="category">
{{category}}
</ion-option>
</ion-select> If |
This worked for me. The selected value is an object
|
Hey ionic team @jgw96 @manucorporat ! This could be fixed by using Angular 4's new |
I'm just going to make a PR for this and use the same logic as Angular's |
I found the same problem. And if the data comes from a promise, it still has error about ngModel binding. |
Why is it always impossible to get a response from the Ionic Team? |
@granthoff1107 You can try |
please note stringfiy is a function that just stringifies whole object. It may pose performance issue. |
The above worked for me too. Thanks! |
The above worked for me too even if I load in the options with an async pipe. |
The above worked for me too! Angular/Ionic should have this built in. |
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. |
Short description of the problem:
cannot select the appropriate option if its values are objects, but the value tied to ngModel is another object with the same properties.
What behavior are you expecting?
I expect the correct option to be selected when the page loads, despite the fact that they are different objects with the same properties.
Assuming I have the following options tied to my ion-select:
and I assign the selected option like this:
It will correctly display the option. However, if I assign it like this (creating a new object with the same properties)
Then the ion-select will be blank
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.
Which Ionic Version?
2.x beta 6
Codepen
http://codepen.io/anon/pen/LNweqo
Run
ionic info
from terminal/cmd prompt: (paste output below)Cordova CLI: 6.0.0
Gulp version: CLI version 3.9.0
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.6
Ionic CLI Version: 2.0.0-beta.25
Ionic App Lib Version: 2.0.0-beta.15
ios-deploy version: 1.8.4
ios-sim version: 5.0.6
OS: Mac OS X El Capitan
Node Version: v4.4.4
Xcode version: Xcode 7.3 Build version 7D175
The text was updated successfully, but these errors were encountered: