2016-11-18 45 views
1

在Angular 2中动态创建时,如何在选择表单上设置特定选项?在Angular 2中动态创建时,如何在选择表单上设置特定选项?

样品typeModel.type =类型: '红汽车'

contactTypes是的类型的数组,类型: '红汽车' 和 '蓝汽车'

<select class="form-control" name="type" [(ngModel)]="typeModel.type"> 
    <option *ngFor="let contactType of contactTypes"> 
     {{contactType.type}} 
    </option> 
</select> 

回答

0

只需添加值以<option>元件通过设置[value]财产,像这样:

<select class="form-control" name="type" [(ngModel)]="typeModel.type"> 
    <option *ngFor="let contactType of contactTypes" [value]="contactType.type"> 
     {{contactType.type}} 
    </option> 
</select> 

(这里的工作方案:http://plnkr.co/edit/JNzyHRHQXxYAWNTcAdT7

相关问题