Tiago Moutinho
Tiago Moutinho

Reputation: 1372

Angularjs 11 iterations issue

Given this example can someone explain to me why angularjs makes always 11 iterations for each $scope function?

Because of that it reaches digest loop limit.

http://plnkr.co/edit/Cy4pytYGH1zLIvvpJIBv?p=preview

Thanks in advance.

edited

$scope.ttotal = function() {
    var ttotal = 0;
    angular.forEach($scope.formData.items, function(item) { 
        ... code here ...
    });
}

Upvotes: 1

Views: 41

Answers (1)

Nikola Yovchev
Nikola Yovchev

Reputation: 10246

It is fairly simple. Both your ttotal() function and the items are on scope. When your template renders, ttotal is called, which increments the items. Since the items are on scope, and angular is greedy in evaling scope changes, it would trigger a template re-rendering, which is going to trigger the ttotal() call again...and so on, until 10/11.

http://plnkr.co/edit/VWXTIZZFZwI1xm0CT5a3?p=preview

This plunkr shows what happens when items are not in the template.

Upvotes: 3

Related Questions