Fisu Fisu - 1 year ago 92
AngularJS Question

AngularJS - How to structure a custom filter with ng-repeat to return items conditionally

I have an ng-repeat that prints list items. I want to write a custom filter so that the list item will print, only if a condition is true.

I seem to have the structure wrong as it seems the variables are not getting passed through to the filter.


<div ng-show="userDetails.username" class="nav">
<li ng-repeat="menuItem in menu | matchAccessLevel:$rootScope.userDetails.accessLevel:menuItem.minAccess | orderBy:'position' ">
<a ng-href="/angular-app/app/{{}}">{{}}</a>


userApp.filter('matchAccessLevel', function() {
return function( item, userAccessLevel, minAccessLevel ) {
if( userAccessLevel >= minAccessLevel ) {
return item;

Answer Source

Filters don't work on individual items in the array, they transform the entire array into another array.

userApp.filter('matchAccessLevel', function() {
  return function( items, userAccessLevel) {
    var filtered = [];
    angular.forEach(items, function(item) {
      if(userAccessLevel >= item.minAccess) {
    return filtered;

See this plnkr

*always inspect the arguments to a function. It's not always obvious what the values are.

see filters guide

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download