Make a negation filter in ng-repeat

Asked

Viewed 629 times

2

I have an object and I’m reading it in ng-repeat, only if the id key equals 0777 I don’t want that position to be displayed,

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

I’m doing fillter normally, displays only the object, but trying to use negation form, does not display all the positions of the object.

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

If anyone has managed to filter with denial in this way and can share, thank you.

1 answer

4


So that the filter is of negation to ! needs to be within string passed to the filter.

Instead of:

!'00777'

Must be:

'!00777'

If you have a variable or function and want to deny the result of the expression, it is necessary 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>
<div ng-app="myApp">
    <div ng-controller="TesteCtrl">
       
        <div ng-repeat="account in accounts | filter:{accountId: '!00777'}">
                    {{account.name}} - {{account.accountId}}
        </div>
    </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.