简体   繁体   中英

Multi checkbox filter not working in ng-repeat with AngularJS

I have several age range filters done in my AngularJS app. Of the 4 of them, one doesn't work properly (results don't match the filter), and I can't seem to find why.

You can see a working example here .

The filters look like this:

.filter('five', function () {
  return function ( items,filter) {
      if(filter)return items.filter(x=>x.age<=4);
      else return items;
  }
})

.filter('child', function () {
  return function ( items,filter) {
      if(filter)return (items.filter(x=>x.age>=5) && items.filter(x=>x.age<=14));
      else return items;
  }
})


.filter('young', function () {
  return function ( items,filter) {
      if(filter)return (items.filter(x=>x.age>=14) && items.filter(x=>x.age<=17));
      else return items;
  }
})


.filter('adult', function () {
  return function ( items,filter) {
      if(filter)return items.filter(x=>x.age>=18);
      else return items;
  }
})

And in the view I do this:

<div ng-repeat="item in data 
    | five:fiveFilter 
    | adult:adultFilter 
    | young:youngFilter 
    | child:childFilter">
 {{item.age}}
</div>

As you will see, all filters work except the young one.

What am I missing?

Change these filters code:

.filter('child', function () {
  return function ( items,filter) {
      if(filter)return (items.filter(x=>x.age>=5 && x.age<=14));
      else return items;
  }
})


.filter('young', function () {
  return function ( items,filter) {
      if(filter)return (items.filter(x=>x.age>=14 && x.age<=17));
      else return items;
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM