Scope life cycle
Scope data goes through a life cycle when the Angular app is loaded into the browser. Understanding the scope life cycle will help you to understand the interaction between scope and other AngularJs components.
The scope data goes through the following life cycle phases.
Creation: This phase initializes the scope. The root scope is created by the $injector when the application is bootstrapped. During template linking, some directives create new child scopes.
A digest loop is also created in this phase that interacts with the browser event loop. This digest loop is responsible for updating DOM elements with the changes made to the model as well as executing any registered watcher functions.
Watcher registration: This phase registers watches for values on the scope that are represented in the template. These watches propagate model changes automatically to the DOM elements.
You can also register your own watch functions on a scope value using the $watch() function.
Model mutation: This phase occurs when data in the scope changes. When you make the changes in your Angular app code, the scope function $apply() updates the model and calls the $digest() function to update the DOM elements and registered watches.
When you do the changes to scope inside your Angular code like within controllers or services, Angular internally call $apply() function for you. But when you do the changes to the scope outside the Angular code, you need to call $apply() function explicitly on the scope to force the model and DOM to be updated correctly.
Mutation observation: This phase occurs when the $digest() function is executed by the digest loop at the end of $apply() call. When $digest() function executes, it evaluates all watches for model changes. If a value has been changed, $digest() calls the $watch listener and updates the DOM elements.
Scope destruction: This phase occurs when child scopes are no longer needed and these scopes are removed from the browsers memory by using $destroy() function. It is the responsibility of the child scope creator to destroy them via scope.$destroy() API.
This stop propagation of $digest calls into the child scopes and allow the memory to be reclaimed by the browser garbage collector.
Scope characteristics
Scopes provide APIs ($watch) to observe model mutations. Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the "Angular realm" (controllers, services, Angular event handlers).
Scopes can be nested to limit access to the properties of application components while providing access to shared model properties. Nested scopes are either "child scopes" or "isolate scopes". A "child scope" (prototypically) inherits properties from its parent scope. An "isolate scope" does not. See isolated scopes for more information. Scopes provide context against which expressions are evaluated. For example, {{username}} expression is meaningless, unless it is evaluated against a specific scope that defines the username property.
Scope Inheritance
Scopes are controllers specific. If we define nested controllers then child controller will inherit the scope of its parent controller.
Let us do a sample and understand what rootscope and scope are.
Example 4
- <html>
- <head>
- <title>Angular JS Forms</title>
- </head>
- <body>
- <h2>AngularJS Sample Application</h2>
- <div ng-app="mainApp" ng-controller="shapeController">
- <p>{{message}} <br/> {{type}} </p>
- <div ng-controller="circleController">
- <p>{{message}} <br/> {{type}} </p>
- </div>
- <div ng-controller="squareController">
- <p>{{message}} <br/> {{type}} </p>
- </div>
- </div>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
- <script>
- var mainApp = angular.module("mainApp", []);
-
- mainApp.controller("shapeController", function ($scope) {
- $scope.message = "In shape controller";
- $scope.type = "Shape";
- });
-
- mainApp.controller("circleController", function ($scope) {
- $scope.message = "In circle controller";
- });
-
- mainApp.controller("squareController", function ($scope) {
- $scope.message = "In square controller";
- $scope.type = "Square";
- });
-
- </script>
- </body>
- </html>
Figure 4: AngularJs Scope
The $rootScope is the top-most scope. An app can have only one $rootScope that will be shared among all the components of an app. Hence it acts like a global variable. All other $scopes are children of the $rootScope.
This is all about the basics of scope in AngularJs. Thanks for reading.