0

A function in my AngularJS controller are invoked multiple times (20+) for each route change. How can I avoid this?

I've made a simple example: http://jsfiddle.net/r6rv078t/3/

Why is this happening, and what are the other digest related pitfalls that an AngularJS developer should be aware of?

   var myController = function ($scope, $location) {
    var _count = 0;
    $scope.locChangeCounter = {
        count: function (viewLocation) {
            console.log(' $location ... ' + $location);
            _count++;
            return _count;
        }
    };
    console.log(' just with in ctrl  ... ' + $location);
};
Patrick Reck
  • 11,246
  • 11
  • 53
  • 86
CoderTR
  • 500
  • 3
  • 7
  • 19
  • 3
    You're binding to `locChangeCounter.count()`, which returns a new value each time it's called. Angular's digest cycle will repeat until all bindings stop changing. – Anthony Chu Oct 29 '14 at 16:48
  • To add to the answer supplied by @AnthonyChu - the digest cycle will run until all bindings stop changing, but Angular will stop trying after a threshold (to avoid infinite looping). The default number of digest iterations is 10 for repeating changes. Your fiddle is hitting this (and the error is in the console). –  Oct 29 '14 at 22:23
  • If you provide your source that's causing the issues, we can likely help troubleshoot the non-fiddle-related issue. –  Oct 29 '14 at 22:27

0 Answers0