我想在数组中重复,但filter
结果只显示与某个属性值匹配的元素。这有可能,怎么样?
这是我正在尝试做的一个例子(显然是失败的):
<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>
$scope.people = [
{name: "John", gender: "male"},
{name: "Sue", gender: "female"},
{name: "Maria", gender: "female"},
{name: "Bob", gender: "male"},
]
因此,第一个列表应该根据属性值打印男性名称和第二个女性名称。
这是否可以使用ng-repeat的过滤器?或者如果没有另外的方法?
提前致谢
答案 0 :(得分:2)
试试这个。 if put:过滤后为true,然后过滤就完全匹配值。
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;