我有 ion-select,它有很多选项,当 View 准备好时,我会根据 CurrentNumber 选择一个默认值。我有这个代码:
<ion-select formControlName="Level">
<ion-option [value]="level.id" *ngFor="let level of levels" [attr.selected]="(level.levelNumber == currentLevel)? true : null">
{{level.name}}
</ion-option>
</ion-select>
this.currentLevel = 1;
数据来自服务器:
data = [
{
levelNumber : 1,
name:'abcd'
},
{
levelNumber:2,
name:'efg'
}
]
最佳答案
代替selected
属性,您可以在数据准备好时在控件中设置默认选项:
// When the data is ready
this.yourForm.get('Level').setValue(data.id);
这会将 id 等于 data.id
的选项设置为默认值。
关于angular - ionic 选择选择默认值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45419910/