AngularJS : Clear $watch AngularJS : Clear $watch angularjs angularjs

AngularJS : Clear $watch


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

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


scope.$watch returns a function that you can call and that will unregister the watch.

Something like:

var unbindWatch = $scope.$watch("myvariable", function() {    //...});setTimeout(function() {    unbindWatch();}, 1000);


You can also clear the watch inside the callback if you want to clear it right after something happens. That way your $watch will stay active until used.

Like so...

var clearWatch = $scope.$watch('quartzCrystal', function( crystal ){  if( isQuartz( crystal )){    // do something special and then stop watching!    clearWatch();  }else{    // maybe do something special but keep watching!  } }