【问题标题】:First item in dropdown is blank when filtering in ng-options在 ng-options 中过滤时,下拉列表中的第一项为空白
【发布时间】:2015-05-30 23:58:15
【问题描述】:

我正在过滤我的 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/

【问题讨论】:

  • 你从哪里得到employees?它是如何填充的?它在原始形式中是什么样的(即其中有哪些项目)?
  • 我编辑了这个问题:D
  • 我非常怀疑它与 Angular、AFAIU 有任何关系,这只是 &lt;select&gt; 的行为。之前已经问过了:stackoverflow.com/questions/12654631/…

标签: angularjs ng-options


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2017-05-17
    相关资源
    最近更新 更多