angularjsangularjs-scope

How do I stop $watching in AngularJS?


I can set up a $watch on an AngularJS scope to be notified when the expression I am interested in has changed. But how do I stop watching once I lose interest?


Solution

  • When calling $watch a function is returned that unregisters the bound expression.

    E.g., to watch a variable foo only change once:

    var unregister = $scope.$watch('foo', function () {
      // Do something interesting here ...
      unregister();
    });