Note that there are some explanatory texts on larger screens.

plurals
  1. POAngular ng-repeat filter with predicate function not works as expected
    primarykey
    data
    text
    <p>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.</p> <p>Basically, the <strong>phone object</strong> used in the tutorial was:</p> <pre><code> { "age": 1, "id": "motorola-xoom", "imageUrl": "img/phones/motorola-xoom.0.jpg", "name": "MOTOROLA XOOM™", "snippet": "The Next, Next Generation..." } </code></pre> <p>What I was trying to do was to add an auto populated select box for order the list:</p> <pre><code> &lt;select ng-model="orderProp"&gt; &lt;option ng-repeat="(key, value) in phones[0]" value="{{key}}"&gt; {{labels[key]}} &lt;/option&gt; &lt;/select&gt; </code></pre> <p>and added a model <strong>labels</strong> to the controller:</p> <pre><code> $scope.labels = { "name": "Phone name", "snippet": "Description", "age": "Newest", }; </code></pre> <p>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 <strong>predicated function</strong> for filtering like this:</p> <pre><code> $scope.isPhonePropFilterable = function (propName) { console.log('it DOES NOT get here!!!'); return propName == 'name' || propName != 'snippet' || propName != 'age'; }; </code></pre> <p>and added this to the ng-repeat</p> <pre><code> &lt;option ng-repeat="(key, value) in phones[0] | filter:isPhonePropFilterable" value="{{key}}"&gt; </code></pre> <p>To my suprise, it was not as easy as I thought, my filter function was not called.</p> <p>See it here: <a href="http://plnkr.co/edit/BlqBc8?p=preview" rel="noreferrer">plunker</a></p> <p>Did I do anything wrong? </p> <hr> <p><strong>edited:</strong> ng-repeat <strong>filter</strong> supports filtering <strong>array</strong> only, not object. The filter function returnes if <strong>array</strong> param is not an array...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload