androberz androberz - 1 month ago 8
AngularJS Question

Filter by property of $scope var in a view

I'm new to angular, and I wanted to use scope variable property in the filter statement in the html view like the following:

<li ng-repeat="item in allItems | filter:{property: !currentItem.property}">{{item.property}}</li>


Here I want to exclude from list of items the item that already displayed:
currentItem
.
But it seems not working for me and I get an empty list.

I was able to implement it by using the filter function:

$scope.exceptCurrentItem = function(anItem) {
return anItem.property != $scope.currentItem.property;
}


What I would like to know, why the first approach is not working for me?

Answer

If you don't want to use a filter expression you can use a simple ng-if.

I've created a plnkr here: https://plnkr.co/edit/mlrxoLUoTuwJaKmsW5IU?p=preview

<li ng-repeat="item in allItems" 
ng-if="item.property != myItem.property">{{item.property}}</li>
Comments