1

I have code like this:

$scope.flags = [];

$scope.$watch('selectedFilter', function() {    
            for (var i = 0; i < 2; i++) {
                $scope.flags.push(i);
            }    
    });

$scope.$watch('flags', function(oldval, newval) {

                    if (oldval != newval) {

                        console.log("hello");
                    }

                });

But I get error:

10 $digest() iterations reached. Aborting!

Why is this ? And how can I come around this problem ? Note this is oversimplification of what I am trying to do :)

hyperN
  • 2,674
  • 9
  • 54
  • 92
  • 1
    take a look: http://stackoverflow.com/questions/14376879/error-10-digest-iterations-reached-aborting-with-dynamic-sortby-predicate – Ivan Chernykh Sep 15 '13 at 16:09

1 Answers1

3

One thing I want to point out is when you watch the change of a list, you should set the 3rd parameter to true.

$scope.$watch('flags', function (oldval, newval) {
    if (oldval != newval) {
        console.log("hello");
    }
}, true); 
zs2020
  • 53,766
  • 29
  • 154
  • 219
  • can you explain **why**? – Simon Nov 08 '13 at 17:38
  • 2
    @simonlchilds The full signarture of $watch is $watch(watchFn, watchAction, deepWatch). The third parameter _deepWatch_ if set, means for Angular: examine **each** property within the object for changes. – Adam Bogdan Boczek Dec 03 '13 at 15:06