Skip to content

Instantly share code, notes, and snippets.

@alcedo
Last active December 23, 2015 03:49
Show Gist options
  • Save alcedo/6576530 to your computer and use it in GitHub Desktop.
Save alcedo/6576530 to your computer and use it in GitHub Desktop.
Angular JS Notes
AngularJS: $watch, $digest and $apply
Some Guidelines For Use:
$watch
DO use $watch in directives to update the DOM when a $scope value changes.
DON'T use $watch in a controller. It's hard to test and completely unnecessary in almost every case. Use a method on the scope to update the value(s) the watch was changing instead.
$digest/$apply
DO use $digest/$apply in directives to let Angular know you've made changes after an asynchronous call, such as a DOM event.
DO use $digest/$apply in services to let Angular know some asynchronous operation has returned, such as a WebSocket update, or an event from a 3rd party library like Facebook API.
DON'T use $digest/$apply in a controller. This will make your code harder to test, and asynchronous operations outside of the Angular framework don't belong in your controllers. They belong in services and directives
http://www.benlesh.com/2013/08/angularjs-watch-digest-and-apply-oh-my.html
=============
JS Promise Pattern:
http://blog.danielpecos.com/2013/01/javascript-promises-101-part2/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment