Assume a list like this:
var list = [
{ status: true, name: 'Bar' },
{ status: true, name: 'Foo' },
{ status: false, name: 'John' }
];
Now I want to list only the names where status === true
So I constructed the following HTML:
<ul>
<li ng-repeat="item in list | completed">
{{item.name}}
</li>
</ul>
And the code for filtering:
angular.module('MyApp')
.filter('completed',
function () {
var filtered = [];
return function (items) {
anuglar.forEach(items, function(item) {
if ( item.status === true ) {
filtered.push(item);
}
}
return filtered;
}
});
What I noticed is that the filter function is called 3 times (which happens to be the length of the list array). But why, shouldn't this be just 1 time ? Is there something wrong in my code because otherwise it doesn't look very optimised ?!