|
Created by James Drummond
over 10 years ago
|
|
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
There are no comments, be the first and leave one below:
Want to create your own Notes for free with GoConqr? Learn more.