ng-repeat中的Angular ng-options

时间:2013-03-20 06:32:09

标签: angularjs

我无法使用ng-options指令将选项元素添加到select元素。但是我能够使用ng-repeat指令完成此操作。为什么呢?

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/

1 个答案:

答案 0 :(得分:5)

请修改以下代码

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