ng-repeat过滤器仅针对具有特定属性值的元素进行迭代

时间:2016-03-06 17:53:09

标签: angularjs filter properties ng-repeat

我想在数组中重复,但filter结果只显示与某个属性值匹配的元素。这有可能,怎么样?

这是我正在尝试做的一个例子(显然是失败的):

HTML(查看)

<ul>
  <li ng-repeat="person in people | filter: person.gender == 'male'"></li>
</ul>

<ul>
  <li ng-repeat="person in people | filter: person.gender == 'female'"></li>
</ul>

JS(控制器)

$scope.people = [

  {name: "John", gender: "male"},

  {name: "Sue", gender: "female"},

  {name: "Maria", gender: "female"},

  {name: "Bob", gender: "male"},

]

因此,第一个列表应该根据属性值打印男性名称和第二个女性名称。

这是否可以使用ng-repeat的过滤器?或者如果没有另外的方法?

提前致谢

1 个答案:

答案 0 :(得分:2)

试试这个。 if put:过滤后为true,然后过滤就完全匹配值。

&#13;
&#13;
function Main($scope) {
    $scope.people = [

  {name: "John", gender: "male"},

  {name: "Sue", gender: "female"},

  {name: "Maria", gender: "female"},

  {name: "Bob", gender: "male"}

];
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <div ng-controller="Main">
      <ul>
  <li ng-repeat="person in people | filter: { gender: 'male' }:true">
   {{person.name}}
  </li>
</ul>

    </div>
</div>
&#13;
&#13;
&#13;