Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch variable and change it

In AngularJS I have a directive that watches a scope variable. When the variable contains certain data then I need to alter that variable a bit. The problem is that when I change the variable that my $watch is triggered again. So I end up in a continuous loop.

scope.$watch('someVar', function(newValue, oldValue) {     console.log(newValue);     scope.someVar = [Do something with someVar]; }); 

This keeps triggering $watch again, which makes sense. But I do need a way to change the watched variable. Is there a way to do this?

like image 280
Vivendi Avatar asked Aug 15 '14 07:08

Vivendi


People also ask

How do you watch a variable in Java?

Setting a Watch on a Variable, Field, or Other Expression To set a watch on an identifier such as a variable or field, right-click the variable or field in the Source Editor and choose New Watch. The identifier is then added to the Watches window. To create a watch for another expression: Choose Run | New Watch.

How to watch a variable in intellij?

Right-click at the end of the desired line and choose Add Inline Watch in the context menu. In the input field that appears, specify an expression to watch. Right-click a variable or expression that you want to watch and choose Add Inline Watch in the context menu.

How to detect change of variable in JavaScript?

Seven methods for detecting a change to a javascript variable. The options include polling, monioring an object's variable, monitoring an array element, and using a CSS animation event to trigger modification to the DOM, and finally using MutationObserver to notify modification to the DOM.

How do you trigger an event when a variable's value is changed JavaScript?

There is no event which is raised when a given value is changed in Javascript. What you can do is provide a set of functions that wrap the specific values and generate events when they are called to modify the values.


1 Answers

When a variable is watched for changes using $scope.$watch, angular checks if the reference has changed. If it has, then the $watch handler is executed to update the view.

If you plan on changing the scope variable within the $watch handler, it will trigger an infinite $digest loop because the scope variable reference changes every time that it is called.

The trick to getting around the infinite digest issue is to preserve the reference inside your $watch handler using angular.copy (docs):

scope.$watch('someVar', function(newValue, oldValue) {     console.log(newValue);     var someVar = [Do something with someVar];      // angular copy will preserve the reference of $scope.someVar     // so it will not trigger another digest      angular.copy(someVar, $scope.someVar);  }); 

Note: This trick only works for object references. It will not work with primitives.

In general, its not a good idea to update a $watched variable within its own $watch listener. However, sometimes it may be unavoidable.

like image 94
pixelbits Avatar answered Oct 04 '22 22:10

pixelbits