@ngdoc overview @name Developer Guide: Scopes @description # What are Scopes? {@link api/ng.$rootScope.Scope scope} is an object that refers to the application model. It is an execution context for {@link expression expressions}. Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. Scopes can watch {@link guide/expression expressions} and propagate events. ## Scope characteristics - Scopes provide APIs ({@link api/ng.$rootScope.Scope#$watch $watch}) to observe model mutations. - Scopes provide APIs ({@link api/ng.$rootScope.Scope#$apply $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 isolate application components while providing access to shared model properties. A scope (prototypically) inherits properties from its parent scope. - Scopes provide context against which {@link guide/expression expressions} are evaluated. For example `{{username}}` expression is meaningless, unless it is evaluated against a specific scope which defines the `username` property. ## Scope as Data-Model Scope is the glue between application controller and the view. During the template {@link compiler linking} phase the {@link api/ng.$compileProvider#directive directives} set up {@link api/ng.$rootScope.Scope#$watch `$watch`} expressions on the scope. The `$watch` allows the directives to be notified of property changes, which allows the directive to render the updated value to the DOM. Both controllers and directives have reference to the scope, but not to each other. This arrangement isolates the controller from the directive as well as from DOM. This is an important point since it makes the controllers view agnostic, which greatly improves the testing story of the applications. function MyController($scope) { $scope.username = 'World'; $scope.sayHello = function() { $scope.greeting = 'Hello ' + $scope.username + '!'; }; }
Your name:
{{greeting}}
In the above example notice that the `MyController` assigns `World` to the `username` property of the scope. The scope then notifies the `input` of the assignment, which then renders the input with username pre-filled. This demonstrates how a controller can write data into the scope. Similarly the controller can assign behavior to scope as seen by the `sayHello` method, which is invoked when the user clicks on the 'greet' button. The `sayHello` method can read the `username` property and create a `greeting` property. This demonstrates that the properties on scope update automatically when they are bound to HTML input widgets. Logically the rendering of `{{greeting}}` involves: * retrieval of the scope associated with DOM node where `{{greeting}}` is defined in template. In this example this is the same scope as the scope which was passed into `MyController`. (We will discuss scope hierarchies later.) * Evaluate the `greeting` {@link guide/expression expression} against the scope retrieved above, and assign the result to the text of the enclosing DOM element. You can think of the scope and its properties as the data which is used to render the view. The scope is the single source-of-truth for all things view related. From a testability point of view, the separation of the controller and the view is desirable, because it allows us to test the behavior without being distracted by the rendering details.
  it('should say hello', function() {
    var scopeMock = {};
    var cntl = new MyController(scopeMock);

    // Assert that username is pre-filled
    expect(scopeMock.username).toEqual('World');

    // Assert that we read new username and greet
    scopeMock.username = 'angular';
    scopeMock.sayHello();
    expect(scopeMock.greeting).toEqual('Hello angular!');
  });
## Scope Hierarchies Each Angular application has exactly one {@link api/ng.$rootScope root scope}, but may have several child scopes. The application can have multiple scopes, because some {@link guide/directive directives} create new child scopes (refer to directive documentation to see which directives create new scopes). When new scopes are created, they are added as children of their parent scope. This creates a tree structure which parallels the DOM where they're attached When Angular evaluates `{{username}}`, it first looks at the scope associated with the given element for the `username` property. If no such property is found, it searches the parent scope and so on until the root scope is reached. In JavaScript this behavior is known as prototypical inheritance, and child scopes prototypically inherit from their parents. This example illustrates scopes in application, and prototypical inheritance of properties. /* remove .doc-example-live in jsfiddle */ .doc-example-live .ng-scope { border: 1px dashed red; } function EmployeeController($scope) { $scope.department = 'Engineering'; $scope.employee = { name: 'Joe the Manager', reports: [ {name: 'John Smith'}, {name: 'Mary Run'} ] }; }
Manager: {{employee.name}} [ {{department}} ]
Reports:
  • {{employee.name}} [ {{department}} ]

{{greeting}}
Notice that Angular automatically places `ng-scope` class on elements where scopes are attached. The `