0

我正在尝试使用“track by”表达式在对象数组中按值跟踪我的选择,但是我无法让它工作。


JS

   var app = angular.module('cwsystem', []).controller('RegistrationCtrl', ['$scope', function($scope){
        $scope.roles = [
            {value: '110', position: 'Security Chief'},
            {value: '111', position: 'Security Officer'}
        ];
   }]);

HTML

 <select name="userStatus" ng-model="user.userStatus" ng-options=" role.value as role.position for role in roles track by role.position" class="form-control input-sm" required>
    <option value="">--Select Status--</option>
 </select>

我希望浏览器呈现这样的结果:

 <select name="userStatus" ng-model="user.userStatus" ng-options=" role.value as role.position for role in roles track by role.position" class="form-control input-sm" required>
    <option value="">--Select Status--</option>
    <option value="110">Security Chief</option>
    <option value="111">Security Officer</option>
 </select>

我可以在这个问题上获得一些帮助吗?

4

1 回答 1

3

ng-options 不接受“role.value as role.position for role in roles track by role.position”表达式。

<select name="userStatus" ng-model="user.userStatus" ng-options=" role.value as role.position for role in roles" class="form-control input-sm" required>
    <option value="">--Select Status--</option>
</select>

如果您在 ng-option 中使用上述表达式,您将在 ng-model 变量中获得选定的值

于 2014-12-27T09:29:39.283 回答