angularjs - Why is my watcher giving me a "Infinite $digest Loop" error? -


i'm having weird issue watcher function:

$scope.$watch('builder.edititemform.quantity',function(newvalue,oldvalue){     if(newvalue !== oldvalue){         if(newvalue % 2 == 0){             builder.edititemform.quantity = newvalue;         } else {             builder.edititemform.quantity = oldvalue;         }     } }); 

i getting error as:

error: $rootscope:infdig infinite $digest loop  10 $digest() iterations reached. aborting! watchers fired in last 5 iterations: [["builder.edititemform.quantity;     newval: 1; oldval: undefined"],["builder.edititemform.quantity; newval: undefined; oldval: 1"],["builder.edititemform.quantity; newval: 1; oldval: undefined"],["builder.edititemform.quantity; newval: undefined; oldval: 1"],["builder.edititemform.quantity; newval: 1; oldval: undefined"]]  https://docs.angularjs.org/error/$rootscope/infdig?p0=10&p1=%5b%5b%22builder.edititemform.quantity;%20newval:%201;%20oldval:%20undefined%22%5d,%5b%22builder.edititemform.quantity;%20newval:%20undefined;%20oldval:%201%22%5d,%5b%22builder.edititemform.quantity;%20newval:%201;%20oldval:%20undefined%22%5d,%5b%22builder.edititemform.quantity;%20newval:%20undefined;%20oldval:%201%22%5d,%5b%22builder.edititemform.quantity;%20newval:%201;%20oldval:%20undefined%22%5d%5d 

i not sure how avoid this. can me out?

since changing value builder.edititemform.quantity inside watch again run digest cycle , watch trigger when value changes . in situation changing value inside watch fall infinite loop.

description

this error occurs when application's model becomes unstable , each $digest cycle triggers state change , subsequent $digest cycle. angular detects situation , prevents infinite loop causing browser become unresponsive.

for example, situation can occur setting watch on path , subsequently updating same path when value changes.

$scope.$watch('foo', function() {   $scope.foo = $scope.foo + 1; }); 

refer doc https://docs.angularjs.org/error/$rootscope/infdig


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -