Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-repeat filter with predicate function not works as expected

This is my first time playing with AngularJS, and actually, I'm following the getting-started tutorial. It came to my mind that I would tweak the tutorial scripts to my understandings, by just adding a little that was not in the tutorial.

Basically, the phone object used in the tutorial was:

    {
        "age": 1, 
        "id": "motorola-xoom", 
        "imageUrl": "img/phones/motorola-xoom.0.jpg", 
        "name": "MOTOROLA XOOM™", 
        "snippet": "The Next, Next Generation..."
    }

What I was trying to do was to add an auto populated select box for order the list:

    <select ng-model="orderProp">
        <option ng-repeat="(key, value) in phones[0]" value="{{key}}">
            {{labels[key]}}
        </option>
    </select>

and added a model labels to the controller:

    $scope.labels = {
        "name": "Phone name",
        "snippet": "Description",
        "age": "Newest",
    };

It was working as expected, except that I only wanted to filter the 3 properties above, so I think it would be easy to add a custom predicated function for filtering like this:

    $scope.isPhonePropFilterable = function (propName) {
        console.log('it DOES NOT get here!!!');
        return propName == 'name' || propName != 'snippet' || propName != 'age';
    };

and added this to the ng-repeat

        <option ng-repeat="(key, value) in phones[0] | filter:isPhonePropFilterable" value="{{key}}">

To my suprise, it was not as easy as I thought, my filter function was not called.

See it here: plunker

Did I do anything wrong?


edited: ng-repeat filter supports filtering array only, not object. The filter function returnes if array param is not an array...

like image 342
Mr. Duc Nguyen Avatar asked Oct 04 '22 13:10

Mr. Duc Nguyen


1 Answers

Well, it was my fault. Ng-repeat filter supports array only, it did only mention array in the docs. And checking the filter function, it returned if array param is not an array....

like image 60
Mr. Duc Nguyen Avatar answered Oct 07 '22 19:10

Mr. Duc Nguyen