在ng-options中过滤时,下拉列表中的第一项是空白的

时间:2015-05-30 13:12:50

标签: angularjs ng-options

我在我的ng-options中过滤这是我的html中的代码

 <select class="form-control" id="selectType" ng-model="user.type" ng-init="user.type= types[0]">
 <option ng-repeat="type in types" value="{{type}}">{{type}}</option>
 </select>

<select class="form-control" ng-model="user.fullname" ng-options="employee as fullName(employee) for employee in employees | filter: user.type">

然后在我的控制器中我有这个代码

$scope.types = ['admin','encoder','checker','cashier']; 

var getEmployees = function () {
    $http.get(httpHost + '/employees').success( function (data) {
      if(data.length !== 0){
        $scope.employees =  $scope.sortData(data,'emp_fname');
        $scope.user.fullname = $scope.employees[0];
        console.log("Employees:");
        console.log($scope.employees);
      }else{
        $scope.noEmployees = true;
      }
    }).error(function (err) {
      console.log(err);
    });
  };

  getEmployees();    

它会正确过滤它,但在下拉列表的第一项中留空 我的结果就像这个http://jsfiddle.net/sheppe/EeL9y/

1 个答案:

答案 0 :(得分:1)

这是由于select的行为。如果要设置初始(非空白)值,可以执行以下操作 -

<select class="form-control" ng-model="user.fullname"
ng-options="employee as fullName(employee) for employee in employees | filter: user.type">
<option value=''>Text you want to display</option>
</select>