0

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?

1

2 Answers 2

1

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>
2
  • Thanks, it works. But why I can't use it using filter?
    – androberz
    Oct 5, 2016 at 14:42
  • you're welcome. in this specific case i think you cannot use an inline filter because you have provided an entire expression, not a simple filter like for example {filter: myproperty}. I think the only way to do it is wrap the functionality in a method of the $scope and bind it to the filter like you did , filter:exceptCurrentItem or use a simple ng-if. cheers
    – Karim
    Oct 5, 2016 at 14:49
0

Please use jquery filter in js files.

$scope.allItems = $filter('filter')($scope.allItems , { property: !$scope.currentItem.property});
1
  • Thanks for your answer, but I'm looking for a way to do it directly in the view.
    – androberz
    Oct 5, 2016 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.