改变角度ng-options的范围

时间:2016-05-05 18:07:43

标签: angularjs ng-options

角色新手,

我有这个:

<div ng-controller="Ctrl">     
Sort by:
<select ng-model="voterList" ng-options="voter as voter.name for voter in voter.availableOptions">           
</select>
<p>Ordered by: {{voterList.name}}</p>

    var app = angular.module('app', []);

function Ctrl($scope) {
  $scope.voter = {
   availableOptions: [
       {id: '1', name: 'Option A'},
       {id: '2', name: 'Option B'},
       {id: '3', name: 'Option C'}
     ],

     }; 
}

我需要将$ scope更改为$scope.voter.DoNotContactBefore而不是$scope.voter,但这似乎与ng-options混乱。我不知道我需要改变什么。

非常感谢,谢谢!

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解你的问题,但我觉得你只需要改写:

$scope.voter = {
   availableOptions: [
       {id: '1', name: 'Option A'},
       {id: '2', name: 'Option B'},
       {id: '3', name: 'Option C'}
     ],

     }; 

为:

$scope.voter = {
   DoNotContactBefore: {
     availableOptions: [
       {id: '1', name: 'Option A'},
       {id: '2', name: 'Option B'},
       {id: '3', name: 'Option C'}
     ]
   }
}; 

然后只需将“ng-options”更改为ng-options="voter as voter.name for voter in voter.DoNotContactBefore.availableOptions"