AngularJS Filter Exact Match

前端 未结 3 1080
臣服心动
臣服心动 2020-12-02 21:58

I need to filter a list of items by their category. I want the user to be able to click a button, and then have the filter applied to a list.

At the moment, I have t

相关标签:
3条回答
  • 2020-12-02 22:32

    This is now provided natively within the filter. You can just pass true to the filter to enable strict matching.

    <li ng-repeat="movie in movieList | filter : filters : true">{{ movie.title }}</li>
    

    Refereces

    https://docs.angularjs.org/api/ng/filter/filter

    https://stackoverflow.com/a/18243147/1466430

    0 讨论(0)
  • 2020-12-02 22:38

    In case someone wants to use the filter on the JavaScript side you can do it like:

    $scope.filtered = $filter('filter')($scope.movieList, { genre.name: filters.genre}, true);
    

    Notice the true at the end... it indicates that is to search for the exact match.

    0 讨论(0)
  • 2020-12-02 22:41

    filter can also take a function where you can implement your own filter. I made a plunker showing it in action: http://plnkr.co/edit/v0uzGS?p=preview

    Here's the relevant code:

    $scope.ChooseGenreFunction = function(genre) {
      $scope.filters = function(movie) {
        return movie.genre === genre;
      };
    };
    $scope.ChooseGenreString = function(genre) {
      $scope.filters = genre;
    };
    
    0 讨论(0)
提交回复
热议问题