离子选择选择默认值不起作用 [英] ion-select select default value not working
本文介绍了离子选择选择默认值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有离子选择,它有很多选项,当视图准备好根据 CurrentNumber 选择一个默认值时我有.我有这个代码:
I have ion-select with that have many options and I have when the view is ready to select one default value depending on CurrentNumber. i have this code:
<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;
数据来自这样的服务器:
the data comes from the server like that :
data = [
{
levelNumber : 1,
name:'abcd'
},
{
levelNumber:2,
name:'efg'
}
]
推荐答案
可以在数据准备好时在控件中设置默认选项,而不是selected
属性:
Instead of the selected
attribute, you can set the default option in the control when the data is ready:
// When the data is ready
this.yourForm.get('Level').setValue(data.id);
这会将 id 等于 data.id
的选项设置为默认值.
That will set the option with id equal to data.id
as the default.
这篇关于离子选择选择默认值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文