In my previous article, I described the basics of AngularJs. You can read it from the following link.

AngularJS - Basics: Part 2

Scopes

A scope is an object that refers to the application model. Pretty vague, right? I'll try to clarify it.

Scopes are the object that provides context to evaluate expressions inside an application model. To be clearer, {{username}} in a template is totally useless unless we have a specific scope object that defines the "username" property. Then only, {{ username }} can be evaluated against this scope object.

The scope has certain characteristics like $watch, $apply, and $digest. What do they do?

It basically works as the glue between the controller and the view, without which, these two cannot communicate or notify each other about changes that are to be reflected. Both the controller and the directive have a reference to scope.

Hierarchy

Every Angular application has only one root scope and may contain several child scopes.

Scopes are arranged in a hierarchical order that is the same as the order of elements in the DOM tree.

When Angular evaluates an expression, it first looks at the scope associated with the current element for any such defined property. If not found, it looks up to the scope of the parent element in the DOM tree and so on until the root scope is reached.

For example

<div ng-controller="oneCtrl">
    {{ username }}
    <div ng-controller="twoCtrl">
        {{ username }}
    </div>
</div>

<script>
    function oneCtrl($scope) {
        $scope.username = "abc";
    }
</script>

This will evaluate both of the {{ username }} expressions since the "username" property is well-defined in the parent scope.

To limit their access to other application components, scopes can be nested.

For example

<div ng-controller="oneCtrl">
    {{ username }}
</div>

<div ng-controller="twoCtrl">
    {{ username }}
</div>

<script>
    function oneCtrl($scope) {
        $scope.username = "abc";
    }
</script>

This time, Angular will not evaluate the second expression under the controller "twoCtrl". The reason is simple, the "username" property is not defined for the scope of the current element. For this to work, we'll need to add.

function twoCtrl($scope) {
    $scope.username = "xyz";
}

Life Cycle

To work well with the scopes, one must have an understanding of all the phases that a scope goes through, during its life cycle.

Directives and Scopes

There are cases when directives create their own scopes. For instance, "ng-controller" and "ng-repeat" create their own child scopes and attach them to the DOM element. But before I get any deeper into it, we all should understand controllers and how to make their best use in applications.

My next extension in this series will talk mainly about controllers.