我正在尝试渲染选择框,但它没有按预期工作 - 选项值不正确。我检查了手册,根据它,数组的语法(在我的例子中是对象数组)是
select as label for value in array
所以这就是我正在做的事情:
数据:
[{"id":"3","name":"asdasd","code":"asdads","group":"2","cost":"0"},{"id":"4","name":"adrf fg df ","code":"dasfasd","group":"2","cost":"0"}]
模板:
<select ng-model="productToBuy" ng-options="item.id as item.id for item in products"></select>
渲染结果:
<select ng-model="productToBuy" ng-options="item.id as item.id for item in products" class="ng-pristine ng-valid">
<option value="0" selected="selected">3</option>
<option value="1">4</option>
</select>
正如我们所看到的,选项值没有设置为项目的 ID。
此外,当源是数组时,这可能不是正确的语法,但我在尝试这样的时候得到相同的结果:
<select ng-model="productToBuy" ng-options="item.id as item.id for (key, item) in products"></select>
我把这段代码放在jsfiddle上。任何帮助表示赞赏。