如何在选定表单上设置特定选项,当它在Angular 2中动态创建时?
示例typeModel.type = type:'Red Cars'
contactTypes是一个类型数组,类型:'Red Cars'和'Blue Cars'
<select class="form-control" name="type" [(ngModel)]="typeModel.type">
<option *ngFor="let contactType of contactTypes">
{{contactType.type}}
</option>
</select>
答案 0 :(得分: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)