我想做类似的事情:
<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.country
为Country
(与form.allCountries
中的项目类型相同),但它是String
。
除了带有选择的String
之外,还有什么办法吗?
感谢您的帮助。
答案 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