How to filter multiple values (OR operation) in angularJS

前端 未结 20 887
清酒与你
清酒与你 2020-11-22 10:01

I want to use the filter in angular and want to filter for multiple values, if it has either one of the values then it should be displayed.

I have for

相关标签:
20条回答
  • 2020-11-22 10:39

    Lets assume you have two array, one for movie and one for genre

    Just use the filter as: filter:{genres: genres.type}

    Here genres being the array and type has value for genre

    0 讨论(0)
  • 2020-11-22 10:41

    I would just create a custom filter. They are not that hard.

    angular.module('myFilters', []).
      filter('bygenre', function() {
        return function(movies,genres) {
          var out = [];
          // Filter logic here, adding matches to the out var.
          return out;
        }
      });
    

    template:

    <h1>Movies</h1>
    
    <div ng-init="movies = [
              {title:'Man on the Moon', genre:'action'},
              {title:'Meet the Robinsons', genre:'family'},
              {title:'Sphere', genre:'action'}
           ];" />
    <input type="checkbox" ng-model="genrefilters.action" />Action
    <br />
    <input type="checkbox" ng-model="genrefilters.family" />Family
    <br />{{genrefilters.action}}::{{genrefilters.family}}
    <ul>
        <li ng-repeat="movie in movies | bygenre:genrefilters">{{movie.title}}: {{movie.genre}}</li>
    </ul>
    

    Edit here is the link: Creating Angular Filters

    UPDATE: Here is a fiddle that has an exact demo of my suggestion.

    0 讨论(0)
提交回复
热议问题