AngularJS - 使用在NG选项$指数 [英] AngularJS - Using $index in ng-options
问题描述
如果我使用绑定数组的SELECT标签以下内容:
If I bind an array to a SELECT tag using the following:
<select ng-model="selData" ng-options="$index as d.name for d in data">
的OPTION标签得到预期的递增索引值(0,1,2,...)。然而,当我从下拉列表中选择的东西, selData code>的值越来越势必
未定义
。应结合实际工作?
The OPTION tags get the expected incrementing index values (0, 1, 2, ...). However, when I select something from the dropdown, the value of selData
is getting bound to undefined
. Should the binding actually work?
在另一方面,如果我做了以下内容:
On the other hand, if I do the following:
<select ng-model="selData" ng-options="d as d.name for d in data">
的OPTION标签得到相同的指标,但整个对象绑定上的改变。我不知道,如果角度是为了支持这一点,或者如果它只是一个不错的错误/副作用。
The OPTION tags get the same index, but the entire object is bound on change. I don't know if Angular is meant to support this, or if it's simply a nice bug/side effect.
推荐答案
$指数是NG重复定义,不要选择。我觉得这说明了未定义
。 (所以,不,这是不应该的。)
$index is defined for ng-repeat, not select. I think this explains the undefined
. (So, no, this shouldn't work.)
角支撑整个对象绑定。文档可以更好的措辞,表明这一点,但它确实暗示了它:ngOptions ...当你想选择模型绑定到一个非字符串值应该用来代替ngRepeat
Angular supports binding on the entire object. The documentation could be worded better to indicate this, but it does hint at it: "ngOptions ... should be used instead of ngRepeat when you want the select model to be bound to a non-string value."
这篇关于AngularJS - 使用在NG选项$指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!