Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : Clear $watch

I have a watch function in my AngularJS application.

$scope.$watch('quartzCrystal', function () {    ... } 

However, after some condition (in my example, changing the page at my single-page application) I want to stop that watch (as like clearing timeout).

How can I do that?

like image 589
kamaci Avatar asked Feb 19 '13 12:02

kamaci


People also ask

How to remove watch in AngularJS?

When you invoke the $watch() method, to create a binding, AngularJS returns a "deregistration" function. This function can then be used to unbind your $watch() listener - all you have to do is invoke this returned function and your $watch() listener will be removed.

What is $Watch in AngularJS?

$watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in Angularjs to handle variable changes in application. Let us understand with the following example to understand how to implement $watch().

What is the second argument in $Watch in AngularJS?

The $watch keep an eye on the variable and as the value of the variable changes the angular JS $what runs a function. This function takes two arguments one is the new value and another parameter is the old value.


1 Answers

$watch returns a deregistration function. Calling it would deregister the $watcher.

var listener = $scope.$watch("quartz", function () {}); // ... listener(); // Would clear the watch 
like image 134
Umur Kontacı Avatar answered Oct 22 '22 07:10

Umur Kontacı