Developer Guide: Scopes: Applying Controllers to Scopes

Work in Progress This page is currently being revised. It might be incomplete or contain inaccuracies.

When a controller function is applied to a scope, the scope is augmented with the behavior defined in the controller. The end result is that the scope behaves as if it were the controller:

var scope = angular.scope();
scope.salutation = 'Hello';
scope.name = 'World';

expect(scope.greeting).toEqual(undefined);

scope.$watch('name', function(){
this.greeting = this.salutation + ' ' + this.name + '!';
});

expect(scope.greeting).toEqual('Hello World!');
scope.name = 'Misko';
// scope.$eval() will propagate the change to listeners
expect(scope.greeting).toEqual('Hello World!');

scope.$eval();
expect(scope.greeting).toEqual('Hello Misko!');

Related Topics

Related API