我试图避免在AngularJS应用程序中多次在同一个集合中使用相同的过滤器。
这样做的正确方法是什么:
<div ng-app="myApp" ng-controller="PeopleCtrl">
<ul ng-init="(person in people|filter:{show:true}) as myPeople" ng-if="myPeople.length>0">
<h4>My People</h4>
<li ng-repeat="person in myPeople">{{person.name}}; display: {{person.show}};
</ul>
</div>
当我使用(person in people|filter:{show:true}) as myPeople
时,我尝试使用别名,以便不在任何地方重复使用相同的过滤器{show:true}
...
答案 0 :(得分:2)
你应该这样做:
<div ng-init="myPeople = (people | filter:{show:true})">
<div ng-if="myPeople.length>0">
<h4> My People</h4>
<ul>
<li ng-repeat="person in myPeople">
{{person.name}}; age: {{person.age}}; show: {{person.show}};
</li>
</ul>
</div>
</div>