Angular dart - 选择Object作为模型(不是字符串)

时间:2018-06-07 07:33:07

标签: angular select dart

我想做类似的事情:

<select id="country" name="select" class="form-control"  [(ngModel)]="form.country">
  <option *ngFor="let country of form.allCountries" [value]="country">
{{country.name}}
  </option>                                                        
</select>

我希望form.countryCountry(与form.allCountries中的项目类型相同),但它是String

除了带有选择的String之外,还有什么办法吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可能需要使用ngValue:

<select id="country" name="select" class="form-control"  [(ngModel)]="form.country">
  <option *ngFor="let country of form.allCountries" [ngValue]="country">
      {{country.name}}
  </option>                                                        
</select>

值与ngValue之间的差异:

[value]="..." only supports string values
[ngValue]="..." supports any type