2013-03-20 60 views
0

我無法使用ng-options指令將選項元素添加到select元素。但是能夠使用ng-repeat指令來做到這一點。爲什麼?ng-repeat內的角度ng選項

HTML,

<div ng-app ng-controller="DescAttrCtrl"> 
    <div ng-repeat="dattr in dattrs"> 
     <div ng-repeat="v in dattr.values"> 
      {{v | json}} 
     </div> 
     <select name="dattr-{{$index}}" ng-options="v for v in dattr.values"> 
       <option value=""> --- select a value --- </option> 
     <!-- <option ng-repeat="v in dattr.values">{{v}}</option> --> 
     </select> 
    </div> 
</div> 

的Javascript,

function DescAttrCtrl($scope) { 
    $scope.dattrs = [ 
    { 
      "name" : "first attribute", 
      "description" : "attribute first description", 
      "values" : ["value1", "value2", "value3", "value4"] 
    }, 
    { 
      "name" : "second attribute", 
      "description" : "attribute second description", 
      "values" : ["value1", "value2", "value3", "value4"] 
    } 
    ]; 
} 

小提琴,http://jsfiddle.net/FwVbC/

+2

你必須指定NG-模型工作 – 2013-03-20 08:23:50

回答

5

請modidy代碼如下

<select ng-model="selection" ng-options="values for values in dattr.values"> 
    </select> 
+0

T hanks Ajay!那完美的工作! – anirudha13 2013-03-21 01:04:29