1

I would like to use my custom filter with ngRepeat directive. Here is what I have

HTML:

<div ng-app="menuApp">
    <ul ng-controller="MenuCtrl">
        <li ng-repeat="item in menuItems | rootCategories">
            {{item.Name}}
        </li>
    </ul>
</div>

JS:

angular.module('menuApp', [])
    .filter('rootCategories', function() {
        return function(item) {
            return item.Parent == 0;
        };
    });

function MenuCtrl($scope) {
    $scope.menuItems = [{ "Id": 1, "Name": "Sweep", "Parent": 0 }];

    /*
    $scope.rootCategories = function(item) {
        return item.Parent == 0;
    };
    */
};

I do not want to use the commented out way to filter my items, because the real filter will be complicated than in the provided example. For some reasons input parameter "item" is not defined, therefore I see nothing. Could you tell me what is wrong? Thank you.

2
  • I have found the following comment: "CORRECTION: the custom filter inside of ng-repeat passes the entire associative array as input, not the key/value of each object, or {{ keyword.values }} as I previously misunderstood." But cannot invent a smart way to apply it. Commented Mar 2, 2013 at 5:49
  • I have found the following link which has helped me to solve my issue: stackoverflow.com/questions/11753321/… Commented Mar 2, 2013 at 6:15

1 Answer 1

5

Please have a look at this fiddle. http://jsfiddle.net/bKFXy/. This uses the syntax of passing a predicate object as the filter expression. item in menuItems | filter:{Parent:0}

There is another method for filtering and the fiddle for that is here http://jsfiddle.net/kd5dv/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.