AngularJS Scope Life Cycle
Posted By : Deepak Mittal | 20-Dec-2014
At the core of AngularJS is the scope life-cycle and so I wanted to take this opportunity to elaborate on some of the fundamental aspects of how scope works in AngularJS.
Few things as it relates to what I want to talk about here are:
- AngularJS compares a value with its previous value and if it has changed then a change event is fired. This is dirty checking in a nutshell.
- In AngularJS, a digest is the cycle that performs dirty checking. This is initiated via $digest().
- If something happens outside of AngularJS, you need to let AngularJS know to execute a digest cycle and you do that via $apply which calls $digest.
Angular.js $watch -
Angular.js $watch event is fired every time, when it detects change on a DOM attribute variable value. For a $watch expression, the listener fire every time when it detect that the current value in context is not same as the previous value. To undetstand the $watch expression we put below a simple working example.
Angular.js $digest
This is the “heartbeat” of an AngularJS application. $digest processes all the watchExpressions for the current scope and its children.So what happens when a watchExpression is processed? I will get into that a bit more when I talk about watchExpressions, but basically the value of the current watchExpression is compared with the value of the previous watchExpression, and if they do not match then it is “dirty” and a listener is fired.With that said, you should not call $digest directly but call $apply which will force a $digest cycle.
Angular.js $apply
$apply enables to integrate changes with the digest cycle
You can think of the $apply
function as of an integration mechanism. You see, each time you change some watched variable attached to the $scope
object directly, Angular will know that the change has happened. This is because Angular already knew to monitor those changes. So if it happens in code managed by the framework, the digest cycle will carry on. However, sometimes you want to change some value outside of the Angular world and see the changes propagate normally. Consider this - you have a $scope.myVar
value which will be modified within a jQuery's $.ajax()
handler. This will happen at some point in future. Angular can't wait for this to happen, since it hasn't been instructed to wait on jQuery. To tackle this, $apply
has been introduced. It lets you to start the digestion cycle explicitly. However, you should only use this to migrate some data to Angular (integration with other frameworks), but never use this method combined with regular Angular code, as Angular will throw an error then.
How all of this is related to DOM?
The digest cycle will make sure that the UI and the JS code stays synced, by evaluating every watcher attached to the all $scope
s as long as nothing changes. If no more changes happen in the digest loop, then it's considered to be finished. You can attach objects to the $scope
object either explicitly in the Controller, or by declaring them in {{expression}}
form directly in the view.
Hope that helps to clarify some basic knowledge about all this
Thanks
Deepak Mittal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Deepak Mittal
Deepak is a bright web Developer. Deepak has expertise in Java.