欢迎加入前端交流群交流知识&&获取视频资料: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

 


 

相关文章:

  • 2023-04-05
  • 2021-08-11
  • 2022-12-23
  • 2018-12-08
  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2018-12-27
  • 2021-08-19
  • 2021-11-06
  • 2021-09-28
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案