AngularJS过滤ngRepeat对没有键的项目

时间:2016-11-09 16:36:28

标签: javascript angularjs

我的数组看起来像这样:

[
  {name:'John',IsManager:true},
  {name:'Terry'},
  {name:'Sam'},
  {name:'Andy'}
]

我试图让它显示 拥有IsManager属性的所有人:Terry, Sam, and Andy

这是我到目前为止所做的:

<span ng-repeat="user in Users | filter: {?????}">
  {{user.name}}
</span>

如何设置此过滤器?这可以在不使用自定义过滤器的情况下完成吗?

2 个答案:

答案 0 :(得分:4)

查看:

<span ng-repeat="user in Users | filter:filterOutNonManagers">
  {{user.name}}
</span>

<强>控制器:

$scope.filterOutNonManagers = function(user) {
    return !user.IsManager;
};

答案 1 :(得分:2)

在这里:(由于下一版1.3.16中修复了错误,因此无法在Angular 1.3.15上运行)

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

app.controller("FooController", function($scope) {

  $scope.Users = [{
    name: 'John',
    IsManager: true
  }, {
    name: 'Terry'
  }, {
    name: 'Sam'
  }, {
    name: 'Andy'
  }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>

<div ng-app="sa" ng-controller="FooController">
  <span ng-repeat="user in Users | filter: {IsManager: '!'}">
      {{user.name}}
    </span>
</div>