2015-04-30 62 views
0

我很努力地理解綁定和選擇元素。我想要兩個select元素進行同步。看起來好像ng-value =「$ index」應該是正確的方式,但它不起作用。角度,同步兩個select元素

這些同步:

<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

我錯過了什麼?

回答

1

裹在括號中的$指數,如{$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> 

http://jsbin.com/pubapigibi/1/edit

在第一個例子
+0

是期權價值asigned關鍵價值和你迭代的數組,鍵值是項目的索引陣列。選擇顯示值是對象「o」(字符串)本身。 –

+0

謝謝,雖然我沒有得到邏輯,也找不到關於該文檔的文檔 – user3057440