I have an ng-repeat
looping through an array of objects that belongs to SomeController
:
<div ng-controller="SomeController as aCtrl">
<div ng-repeat="category in aCtrl.categories">
<p ng-show="aCtrl.checkShouldBeDisplayed(category)">{{category.name}}</p>
</div>
</div>
The controller is defined as:
app.controller('SomeController', function() {
this.categories = [{
"name": "one",
}, {
"name": "two",
}, {
"name": "three",
}, {
"name": "four",
}];
this.counter = 0;
this.checkShouldBeDisplayed = function(channel) {
this.counter = this.counter + 1;
console.log("executing checkShouldBeDisplayed " + this.counter);
return true;
};
});
I would expect the checkShouldBeDisplayed
function to count to 4 as there are four elements in the SomeController.categories
array. Instead it counts to 8 - check it here: http://plnkr.co/edit/dyvM49kLLTGof9O92jGb (you'll need to look at the console in your browser to see the log). How can I get around this behaviour? Cheers!