Angularjs filter negated

后端 未结 6 680
迷失自我
迷失自我 2020-11-30 10:01

I have a set of items that I want to filter in ng-repeat using an ng-model as the string to filter the set, so far I haven\'t found a way to make it work when the expression

相关标签:
6条回答
  • 2020-11-30 10:18

    If using objects, you might also be interested in the following:

    <li data-ng-repeat="obj in objs | filter:({obj_attr: '!obj_val'})">
       ...
    </li>
    

    Tested in AngularJS 1.1.5.

    0 讨论(0)
  • UPDATE: see ENDOH takanao's answer.

    Looking at the AngularJS source code, it appears that '!' negates the result of the predicate, not the predicate itself:

    var search = function(obj, text){
        if (text.charAt(0) === '!') {
           return !search(obj, text.substr(1));
        }
        switch (typeof obj) {
        ...
    

    So, one way to work around this is to [If you don't like the '!'+myFilter syntax,] you can define your own predicate function in your controller:

    $scope.inverseOriginFilter = function(item) {
        return item.search($scope.languageOrigin) == -1
    }
    

    Then use it in your HTML:

    <select ng-model="languageDestination" ng-change="updatePrice()" 
        ng-options="language for language in languages | filter:inverseOriginFilter">
    </select>
    

    Example fiddle.

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

    If you're using a method to filter than prefixing the method name with '!' will not work. Instead you can do something like:

    // 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 do:

    filter:not(myFilterMethod)
    

    In your html it would look like:

    <select ng-model="languageDestination" ng-change="updatePrice()">
      <option ng-repeat="language in languages | filter:not(languageOrigin)">{{language}}</option>
    </select>
    

    Reference: https://stackoverflow.com/a/17811582/175830

    0 讨论(0)
  • 2020-11-30 10:30

    I'm using 1.3.15 and ENDOH's solution did not work. Instead, filter:'!':languageOrigin worked for me.

    0 讨论(0)
  • 2020-11-30 10:33

    To completely negate the $filter filter, i add this filter:

    .filter('$nfilter', ['$filter', function($filter) {
        return function() {
            var itemsToExclude = $filter('filter').apply(null, arguments);
            return arguments[0].filter(x => !itemsToExclude.includes(x));
        }
    }])
    

    in template, we can write:

    <ui-select-choices repeat="type.id as type in data.types | $nfilter:{id: form.typeId}:true | filter: {label: $select.search}">
        <div ng-bind-html="type.label | highlight: $select.search"></div>
    </ui-select-choices>
    
    0 讨论(0)
  • 2020-11-30 10:37

    '!' character prepend to the filter string, like below:

    filter:'!'+languageOrigin

    <select ng-model="languageOrigin" ng-change="updatePrice()">
      <option ng-repeat="language in languages">{{language}}</option>
    </select>
    <select ng-model="languageDestination" ng-change="updatePrice()">
      <option ng-repeat="language in languages | filter:'!'+languageOrigin">{{language}}</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题