角度飞镖 - 选择对象作为模型(不是字符串)



我想做这样的事情:

<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之外,还有什么方法可以拥有其他东西吗?

感谢您的帮助。

您可能需要使用 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>

值和ng值之间的区别:

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

最新更新