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

时间:2016-11-18 00:13:21

标签: angular

如何在选定表单上设置特定选项,当它在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>

1 个答案:

答案 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