AngularJS : Clear $watch

kamaci picture kamaci · Feb 19, 2013 · Viewed 120.3k times · Source

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?

Answer

Umur Kontacı picture Umur Kontacı · Feb 19, 2013

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

var listener = $scope.$watch("quartz", function () {});
// ...
listener(); // Would clear the watch