我正在努力理解绑定和选择元素。我想要同步两个选择元素。似乎ng-value =“$ index”应该是正确的方法,但它不起作用。
这些同步:
<select ng-model="myVar1"><option value="{{n}}" ng-repeat="(n, o) in ['donny', 'felix', 'bob'] track by $index" >{{o}}</option></select>
{{Ò}}
但这些不是:
<select ng-model="myVar2"><option ng-value="$index" ng-repeat="o in ['donny', 'felix', 'bob'] track by $index" >{{o}}</option></select> <select ng-model="myVar2"> <option ng-value="$index" ng-repeat="o in ['donny', 'felix', 'bob'] track by $index" >{{o}}</option> </select>
JSBIN: http://jsbin.com/dirugikice/1/edit?html,js,output
我错过了什么?
答案 0 :(得分:1)
将$ index包含在括号中,例如{$index}
<select ng-model="myVar2"><option ng-value="{$index}" ng-repeat="o in ['donny', 'felix', 'bob'] track by $index" >{{o}}</option></select> <select ng-model="myVar2"> <option ng-value="{$index}" ng-repeat="o in ['donny', 'felix', 'bob'] track by $index" >{{o}}</option> </select>