Reverse polarity of an angular.js filter

前端 未结 4 792
离开以前
离开以前 2020-12-05 22:56

Given a filter method that returns true if some condition is met, is it possible to invoke its opposite in HTML, e.g. to use:

\"item in items | filter:!AllDa         


        
相关标签:
4条回答
  • 2020-12-05 23:51

    Another option is to create a new negation filter and pipe it:

      .filter('not', function() {
        return function(input) {
          return !input;
        }
      })
    
    "item in items | filter:AllDay | filter:not"
    
    0 讨论(0)
  • 2020-12-05 23:54

    shacker's answer didn't work for angular 1.0.7, so here's another way to do this:

    // You can register this in your AppCtrl if you like, otherwise just use $scope.
    $rootScope.not = function(func) {
        return function (item) { 
            return !func(item); 
        }
    };
    

    Then you would do this:

    filter:not(myFilterMethod)
    
    0 讨论(0)
  • 2020-12-05 23:57

    As noted by ENDOH (this SO question is technically a duplicate), you can negate a filter by prepending '!' to the filter string, like this:

    filter:'!'+myFilter
    

    Note that the '!' is quoted. The documentation is not terribly clear on this, and an example there would be helpful.

    0 讨论(0)
  • 2020-12-06 00:02
    filter:({day: '!'+AllDay.day})
    
    0 讨论(0)
提交回复
热议问题