Samantha J T Star
Samantha J T Star

Reputation: 32758

How can I stop watching a value in AngularJS?

I have the following:

$scope.$watch('city', function (newValue, oldValue) {

But I want to stop the watch when I populate a new city list. How can I remove the watch ?

Upvotes: 0

Views: 214

Answers (1)

chr1s1202
chr1s1202

Reputation: 467

$watch returns a function to remove the added event listener.

So you must save the returned function from your watch execution to later disable the watch via the returned function.

Here is an example:

var myWatchFn = $scope.$watch('myVariable', myWatcher);

// Remove watch.
myWatchFn();

Upvotes: 8

Related Questions