Serge Serge - 2 years ago 57
AngularJS Question

Avoid multiple use of a Filter in AngularJs

I am trying to avoid using the same filter on the same collection multiple times in AngularJS application.

What is the correct way to do it:

<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">{{}}; display: {{}};


When I use
(person in people|filter:{show:true}) as myPeople
I try to use an alias in order to not repeat the same filter

Answer Source

You should do it something like this:

<div ng-init="myPeople = (people | filter:{show:true})">
  <div ng-if="myPeople.length>0">
    <h4> My People</h4>
      <li ng-repeat="person in myPeople">
        {{}}; age: {{person.age}}; show: {{}};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download