【发布时间】:2016-09-12 08:40:05
【问题描述】:
我的用例: 我正在使用 ngFor 将对象数组分配给 select 的选项,如下所示
<select>
<option *ngFor="let item of data" [value]="item.key">{{item.value}}</option>
<select>
我的数据是
[
{
"key" : "CHV",
"value" : "Chivas"
},
{
"key" : "BLN",
"value" : "Balentines"
},
{
"key" : "BDG",
"value" : "Black Dog"
}
]
相当直截了当,现在我想要绑定用户指定的 item.key 和 item.value,即他应该能够提及选项值和标签使用哪个。这是我尝试过但没有用的方法
<option *ngFor="let item of data; let itemlabel = optionlabel; let itemvalue = optionvalue" value="{{item.itemvalue}}">{{item.itemlabel}}</option>
optionvalue & optionlabel 是组件中的字符串,如下所示
optionvalue : string = 'key';
optionlabel : string = 'value';
所以现在当我交换它们时,我可以将键作为值,反之亦然。任何想法如何做到这一点
【问题讨论】:
标签: angular typescript