The following HTML code populates a ul with 21 phones:
<li ng-repeat="phone in phones" ng-class="{'digestTest': countDigestOccurences(phone) }">
<p>{{phone.snippet}}</p>
</li>
countDigestOccurences
is a JavaScript method which uses a dictionary to keep track of how many times countDigestOccurences()
is called per phone.
$scope.countDigestOccurences = function(phone){
var phoneFound = false;
$.each($scope.digestOccurencesPerPhone, function(){
if(this.phone.id == phone.id){
phoneFound = true;
this.occurences++;
}
});
if(!phoneFound)
{
$scope.digestOccurencesPerPhone.push({
phone: phone,
occurences: 1
});
}
}
Through this method I can clearly see that countDigestOccurences is called 4 times per phone. I can not, for the life of me, figure out why it's called 4 times.
Update:
Number of cycles will remain 4 even if the Phone item's HTML is as follows:
<li ng-repeat="phone in phones "
class="thumbnail phone-listing" ng-class="{ 'digestTest': countDigestOccurences(phone), 'digestTestAgain': randomMethodDoesNothing() }">
<p>{{phone.snippet}}</p>
</li>