如何在ng-options中设置值选项而不是数组索引?

时间:2015-06-03 15:13:42

标签: angularjs

我使用ng-options的下一个构造:

ng-options="month.id as month.value for month in data.dateList.years"

它给了我带有值的selcet列表:

<option value="0" label="2016">2016</option>

如何为每个选项设置2016而不是value = "0"

3 个答案:

答案 0 :(得分:1)

<强> HTML

    <div ng-app="myapp">
    <fieldset ng-controller="FirstCtrl">
        <select 
            ng-options="month.value as month.value for month in year track by month.value"
            ng-model="selectedValue"></select>
        <br/>
        Month is: {{ selectedValue }}
    </fieldset>
</div>

<强> JS

var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
    $scope.year = 
     [{id: 1, value: "January"}, 
      {id: 1, value: "February"}];
});

JSFIDDLE看到它正在运行

答案 1 :(得分:0)

而不是err = _error.message使用month.id

month.value

答案 2 :(得分:0)

尝试在month.value

中使用month.id代替ng-options