欢迎加入前端交流群交流知识&&获取视频资料:749539640
<h1>My Application</h1> <select [(ngModel)]="selectedValue"> <option *ngFor="let c of countries" [ngValue]="c">{{c.name}}</option> </select>
ts:
countries:Array<Object> = [
{num: 0, name: "AA"},
{num: 1, name: "BB"}
];
c = this.countries[0];
注意: 你可以用 [ngValue]="c"
替换[ngValue]="c.id"
当c是一个对象
[value]="..."
只支持字符串[ngValue]="..."
支持任何类型
如何value是对象,ngModel绑定的值也需要是相同的对象!
参考:https://github.com/angular/angular/issues/13268