How to call any function as a $filter in AngularJs
Recently I had this little trouble. I had a simple angular controller, which filtered some array of items. I did not want to define the filter in a “global space” because I knew it will be used only by this one controller. So this first thing was to define the custom filter function. Like this
$scope.customFilter = (item)=> {
return item.someProperty == $scope.filterValue;
};
Then call in UI like this.
<htmlElement ng-repeat="item in itemList | filter:customFilter"></htmlElement>
So far nothing special, just what is in the angular documentation. But I needed to call that custom filter again, but not from UI but programaticaly. The way how to invoke custom filter without registering the function via myAngularModule.filter(‘customFilter’, function() {…}) is this.
var filteredItems = $filter('filter')($scope.itemList, $scope.customFilter)
Written by David Votrubec
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Angularjs
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#