I'm using Ionic v2 and I can not set the selected value when showing the page.
<ion-select [(ngModel)]="company.form">
<ion-option *ngFor="let form of forms" [value]="form" [selected]="true">{{form.name}}</ion-option>
</ion-select>
I've tried with checked
, too but that isn't work either. How can I do this?
Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.13
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.45
the following will not work:
but the following will work:
The select option values are treated as string, so you should assign string values to your model variable so that comparison will not fail.
ion Select tag Just Assign the array to ngModel and default value is selected. Simple
The problem seems to be that ion-option don't like objects in rc3. I have to work with only the id part of the object and write a seperate changehandler that find the needed object and set it as a value.
And the changehandler:
This seems to be a bug in rc3 but I don't know where can I report bugs. I did open a topic on ionic forum.
If you deal with default value xor objects, the cleanest way is to provide a compare function to the [compareWith] attribute. This function takes 2 objects in parameters and returns true if they are equals. This way, existing values in model will be selected at start. This works too if new data are added in model outside of the select.
Following example is taken from official Ionic doc
Inside ion-option, you can do