ionic 3:如何将<ion-option>设置为选中状态?

时间:2018-09-15 11:08:44

标签: angular ionic3

如何设置带有离子3和角度的动态生成的选择控件中选择的选项?

如果以前没有选择“我想将其设置为默认状态”,但我无法这样做。

<ion-item>
<ion-label stacked color="primary">State</ion-label>
<ion-select [(ngModel)]="signup.state" name="state" #state = "ngModel" (ionChange)="getcitylist(signup.state)" placeholder="Please Select State">
    <ion-option *ngIf="signup.state==0" value="0" [selected]="true">Select State</ion-option>
    <ion-option *ngFor="let state of statelist" value="{{state.state_id}}">{{state.state_name}}</ion-option>
</ion-select>

问题在于如何将一个选项设置为选中状态,我似乎无法弄清楚。

1 个答案:

答案 0 :(得分:2)

使用[value]="state.state_id"代替value="{{state.state_id}}"

Stackblitz Demo

<ion-item>
<ion-label stacked color="primary">State</ion-label>
<ion-select [(ngModel)]="signup.state" (ionChange)="getcitylist(signup.state)" placeholder="Please Select State">
    <ion-option *ngIf="signup.state==0" value="0" [selected]="true">Select State</ion-option>
    <ion-option *ngFor="let state of statelist" [value]="state.state_id">{{state.state_name}}</ion-option>
</ion-select>