避免在AngularJs中多次使用Filter

时间:2016-06-20 12:59:56

标签: angularjs

我试图避免在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>

CODE PEN

当我使用(person in people|filter:{show:true}) as myPeople时,我尝试使用别名,以便不在任何地方重复使用相同的过滤器{show:true} ...

1 个答案:

答案 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>