AngularJS: How to remove a watch

Updated . Posted . Visible to the public.

Sometimes you want Angular to watch an object only until a certain state is reached (e.g. an object appears in the scope).

Angular's $watch returns a method that you can call to remove that watch. For example:

unwatch = $scope.$watch 'user', (user) ->
  if user?
    ... # do something
    unwatch()

That's it.

Profile picture of Arne Hartherz
Arne Hartherz
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2014-12-12 09:59)