Make a negation filter in ng-repeat

2

I have an object and I'm reading it in ng-repeat, but if the id key is equal to 0777 I do not want this position to be displayed,

<div ng-repeat="account in accounts | filter:{accountId :'00777'} ">
                {{account.name}} - {{account.accountId}}
</div>

I'm doing the fillter normally, it only displays the object, but trying to use it in a denial way, does not display all the positions of the object.

<div ng-repeat="account in accounts | filter:{accountId :!'00777'} ">
                {{account.name}} - {{account.accountId}}
</div>

If someone has already managed to deny filtering in this way and can share it, thank you.

    
asked by anonymous 23.02.2015 / 17:19

1 answer

3

In order for the filter to be denied, the ! must be in of the string passed to the filter.

Instead of:

!'00777'

It should be:

'!00777'

If you have a variable or function and want to negate the result of the expression, you need to concatenate a ! with the item in the declaration:

filter:{accountId: '!'+algumaFuncao}

An example:

angular.module('myApp', [])
.controller('TesteCtrl', ['$scope', function($scope) {
    $scope.negateAccountId = function(item) {
        return item.accountId != '00777';
    };
    
    $scope.accounts = 
        [
            {
                name: "Teste 1",
                accountId: '00111'
            },
            {
                name: "Teste 2",
                accountId: '00777'
            },
            {
                name: "Teste 3",
                accountId: '00444'
            },
            
        ]
    ;
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="myApp">
    <div ng-controller="TesteCtrl">
       
        <div ng-repeat="account in accounts | filter:{accountId: '!00777'}">
                    {{account.name}} - {{account.accountId}}
        </div>
    </div>
</div>
    
23.02.2015 / 17:43