Skip to content

Instantly share code, notes, and snippets.

@christoomey
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christoomey/9377095 to your computer and use it in GitHub Desktop.
Save christoomey/9377095 to your computer and use it in GitHub Desktop.
Angular $watch gotcha

Turns out that angular $watch statements will always fire once to "initialize" the 'watch' statement. The officially recommended workaround, should you not want this behavior, is to use an object identity check to compare the values, ie:

$scope.$watch 'allProviders', (selected, prior) ->
  return if selected is prior
  ...

In JS, the === is the same as Coffeescripts is operator.

Note, this has nothing to do with the third argument to $watch which tells the $watch command to compare on identity vs equality. The initialization behavior described above will occur regardless of this setting.

From the docs:

After a watcher is registered with the scope, the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In rare cases, this is undesirable because the listener is called when the result of watchExpression didn't change. To detect this scenario within the listener fn, you can compare the newVal and oldVal. If these two values are identical (===) then the listener was called due to initialization.

  • Angularjs docs for $watch
@mxie
Copy link

mxie commented Mar 5, 2014

Could you also comment on the use of the identity vs. equality comparison in combination with the guard suggested above?

@christoomey
Copy link
Author

The guard and the identity vs equality described above are completely independent. Regardless of the $watch mode used (identity or equality), the $watch statement will be executed once on creation to initialize it.

In both cases, you want to use an identity check (is in coffeescript, or === in JS) for the guard clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment