Are you absolutely certain that the residence control has its initial value set to “Self” by the time the template accesses it (by which I pretty much mean “in the component constructor”)?
Not so “Self” but to “0”. Yes it is set. Setting it to “1” successfully sets the Spouse as the default in the dropdown, with same issue of not showing it before clicking the select.
Might be a little late to the game here, but ran into this issue today as well and I have come up with a workaround albeit not for all situations.
If in your TS file, you can push the options you want to an array variable, you can access them via interpolation and it adds to the form builder upon submission.
something like this in your TS (for loop here) this.testArray.push(data[i]);
in your HTML <ion-option *ngFor="let item of testArray; let i = index " value="{{item}}" selected="i === 0">{{item}}</ion-option>
simple solution but requires a little extra legwork. Frustrating that it doesnt work like expected.
Wao thank you so much. This answered my question about ion-select with formBuilder approach. now i do array.map() in the form init for the auto select of values present in the options.
I simply reset the values again in on the form object…
async ionViewDidLoad(){
await this.init();
this.userDetailsForm.controls[‘institution’].setValue(this.institution);
this.userDetailsForm.controls[‘campus’].setValue(this.firstTimeCampus);
this.userDetailsForm.controls[‘college’].setValue(this.firstTimeCollege);
this.userDetailsForm.controls[‘department’].setValue(this.firstTimeDepartment);
this.userDetailsForm.controls[‘userStatus’].setValue(this.userStatus);
this.userDetailsForm.controls[‘userType’].setValue(this.userType);
this.userDetailsForm.controls[‘role’].setValue(this.role);
}
This gave me the inspiration for my solution. Thanks!
The problem (I think) is that Ionic automatically decides what option to mark as selected only if the option’s value matches what you interpolate. Which is why @tenonic 's final solution worked.
With your solution, you manually decide when each option is selected. So if you don’t want to, you don’t have to put your options in an Array and loop for this to work. Say you have:
<ion-select [(ngModel)]="employment" placeholder="Are you employed?">
<ion-option value="self-employed">Yes, I work for myself</ion-option>
<ion-option value="employed">Yes, I work for a company</ion-option>
<ion-option value="student">No, I'm a student</ion-option>
</ion-select>
By default, when you select any of these options, you won’t see your selected value, but once you do this, you should be good:
<ion-select [(ngModel)]="employment" placeholder="Are you employed?">
<ion-option value="self-employed" selected="employment === 'self-employed'">Yes, I work for myself</ion-option>
<ion-option value="employed" selected="employment === 'employed'">Yes, I work for a company</ion-option>
<ion-option value="student" selected="employment === 'student'">No, I'm a student</ion-option>
</ion-select>
Super embarassing answer: in one case everything worked correctly - but my css was making the text the same color as the background. Hate when that happens.