Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Angular data binding: $apply and $digest

Description

A-Level Angular Note on Angular data binding: $apply and $digest, created by James Drummond on 29/12/2014.
James Drummond
Note by James Drummond, updated more than 1 year ago
James Drummond
Created by James Drummond over 10 years ago
3
0
1 2 3 4 5 (0)

Resource summary

Page 1

Data binding: 'bind' parts of the HTML interface (the view) to the underlying data (the model)

Views are interpolated when the view is evaluated with one or more variable substitutions; the result is that the variables in our string are replaced with values.

Automatic data binding gives us the ability to consider the view to be a projection of the model state. Any time the model is changed in the client-side model, the view reflects these changes without writing any custom code. It just works.

When Angular thinks that a value might change, it will run its own event loop to check if the value is dirty. A value is considered dirty if it has changed since the last time the event loop ran. This is how Angular is able to track and respond to changes in the app The event loop is called the $digest() loop, which is covered in detail in the digest loop chapter.

See: http://www.sitepoint.com/understanding-angulars-apply-digest/

Hmm... As a test for myself, here we go (please feel free to correct):1. Model is created in a view, one way data-binding set up between {{myModel}} and $scope.myModel .2. How does this work? How do we know when $scope.myModel has changed and trigger an update on {{myModel}}?3. Answer: Angular has attached $scope.$watch to $scope.myModel with a callback that will return any changes to $scope.myModel.4. But... A change to $scope.myModel may affect other models. To manage this, Angular implements the $digest cycle (I suppose you could say that Angular is 'digesting' all of the changes).5. The $digest cycle is triggered by certain built-in events (i.e. changes to a model in the view) or you can trigger it explicitly using $scope.$apply(). It may be necessary to call $scope.$apply explicitly under certain conditions (e.g. If you're using setTimeout()).6. The $digest cycle executes all of the $scope.$watch functions to see if any of the models have changed and updates views accordingly. Since some models may affect others, it repeats this process until no $watch functions are reporting any changes (this is called 'dirty checking' ) or it has been run 10 times at which point an exception will be thrown.

New Page

Show full summary Hide full summary

0 comments

There are no comments, be the first and leave one below:

Similar

Angular
Kingsley Bawuah
Angular Nuts and Bolts
James Drummond
Scope and Inheritance in Angular
James Drummond
Angular application scaffolding
James Drummond
Directives and scope
James Drummond
FE Web Applications Components
Juan Carlos Joya Carvajal
Fluxo buscaCliente
Alex José Silva
How Angular works
James Drummond
Angular-hint
James Drummond
Angular Concept questions and answers
Abinaya Murugaiyan