如何在angularjs的选择框中显示选项?

时间:2016-04-20 05:14:41

标签: angularjs

我无法在angularjs中设置选项

<tr ng-repeat="leader in filtered = (list | orderBy:sort.active:sort.descending) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
    <td>
       <select class="form-control mt10" name="leaderRole" ng-model="leader.role_id" ng-class="{ inputerror: !leader.role_id}">
          <option value="">Please select role</option>
          <option ng-repeat="role in employeeRole" ng-selected="{{leader.role_id == role.id}}" value="{{role.id}}">{{role.description}}</option>
       </select>
    </td>
</tr>

控制器部分

var leaders_list = angular.module('leaders_list'); //getter
leaders_list.controller('LeaderController',['$scope','$http','$filter',LeaderController]);
function LeaderController($scope,$http,$filter)
{
   $scope.list = [
      {
        "title": "XYZ",
        "name_first": "ABC",
        "name_last": "EFG",
        "email": "lmn@opq.com",
        "id": 1,
        "role_id": 3
      }
   ];

  $scope.employeeRole = [
    {
      "id": 2,
      "description": "JKL",
      "is_active": true
    },
    {
      "id": 3,
      "description": "STV",
      "is_active": true
    }
  ];
}

这里的leader.role_id是每个领导者的role_id的值,当我在上面的代码中执行时,它没有显示我选择的值。

如果我从选择框中删除ng-model,它会将我的值显示为已选中。

Plunker Link: https://plnkr.co/edit/qesu3J6mxJdS6bTjPXKI?p=info

谁能建议我怎么做?

1 个答案:

答案 0 :(得分:1)

如果不按照建议使用ng-options,你会使自己的生活变得困难。

只需用

替换你的选择
<select class="form-control mt10" name="leaderRole" 
        ng-model="leader.role_id" ng-class="{ inputerror: !leader.role_id}"
        ng-options="role.id as role.description for role in employeeRole">
  <option value="">Please select role</option>
</select>

https://plnkr.co/edit/fW9O6BCDOl59aziTz7zX?p=preview