1

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 ...
    });
}

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok i understand now, its beacuse they are in the scope. But what if i have a list (ng-repeat) then with that function i want to sum some values that are on the same scope as well. it gives me that same 10/11 iterations. Do you have some solution for that?
i have edited my question with some code that gives the same error.
You shouldn't use a function as a return value in a template. You'd expect to attach this function on an ng-click or something, but not rendered in the template. You shouldn't put functions in your template for rendering purposes, especially if those functions affect other stuff on scope. You can try using ng-init, but i am not sure it is nice.
my goal is to dynamically change some value in the template using simple scope binding.
I seriously have no idea what you want to do...take a look at this plunkr: plnkr.co/edit/zortRo5XqzFeAdNP2sx2?p=preview
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.