View source Improve this doc

Scope

type in module ng

Description

A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created automatically when compiled HTML template is executed.)

Here is a simple scope snippet to show how you can interact with the scope.

  1. var scope = $rootScope.$new();
  2. scope.salutation = 'Hello';
  3. scope.name = 'World';
  4.  
  5. expect(scope.greeting).toEqual(undefined);
  6.  
  7. scope.$watch('name', function() {
  8. scope.greeting = scope.salutation + ' ' + scope.name + '!';
  9. }); // initialize the watch
  10.  
  11. expect(scope.greeting).toEqual(undefined);
  12. scope.name = 'Misko';
  13. // still old value, since watches have not been called yet
  14. expect(scope.greeting).toEqual(undefined);
  15.  
  16. scope.$digest(); // fire all the watches
  17. expect(scope.greeting).toEqual('Hello Misko!');
  18.  

Inheritance

A scope can inherit from a parent scope, as in this example:

  1. var parent = $rootScope;
  2. var child = parent.$new();
  3.  
  4. parent.salutation = "Hello";
  5. child.name = "World";
  6. expect(child.salutation).toEqual('Hello');
  7.  
  8. child.salutation = "Welcome";
  9. expect(child.salutation).toEqual('Welcome');
  10. expect(parent.salutation).toEqual('Hello');

Usage

  1. Scope([providers][, instanceCache]);

Parameters

ParamTypeDetails
providers (optional) Object.<string, function()> Map of service factory which need to be provided for the current scope. Defaults to ng.
instanceCache (optional) Object.<string, *> Provides pre-instantiated services which should append/override services provided by providers. This is handy when unit-testing and having the need to override a default service.

Returns

Object Newly created scope.

Methods

  • $apply(exp)

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.

Life cycle

Pseudo-Code of $apply()

  1. function $apply(expr) {
  2. try {
  3. return $eval(expr);
  4. } catch (e) {
  5. $exceptionHandler(e);
  6. } finally {
  7. $root.$digest();
  8. }
  9. }

Scope's $apply() method transitions through the following stages:

  • The expression is executed using the $eval() method.
  • Any exceptions from the execution of the expression are forwarded to the $exceptionHandler service.
  • The watch listeners are fired immediately after the expression was executed using the $digest() method.
Parameters

ParamTypeDetailsexp (optional) stringfunction()

An angular expression to be executed.

  • string: execute using the rules as defined in expression.
  • function(scope): execute the function with current scope parameter.
Returns

*

The result of evaluating the expression.

  • $broadcast(name, args)

Dispatches an event name downwards to all child scopes (and their children) notifying the registered ng.$rootScope.Scope#$on listeners.

The event life cycle starts at the scope on which $broadcast was called. All listeners listening for name event on this scope get notified. Afterwards, the event propagates to all direct and indirect scopes of the current scope and calls all registered listeners along the way. The event cannot be canceled.

Any exception emitted from the listeners will be passed onto the $exceptionHandler service.

Parameters

ParamTypeDetailsnamestring

Event name to broadcast.

args…*

Optional set of arguments which will be passed onto the event listeners.

Returns

Object

Event object, see ng.$rootScope.Scope#$on

  • $destroy()

Removes the current scope (and all of its children) from the parent scope. Removal implies that calls to $digest() will no longer propagate to the current scope and its children. Removal also implies that the current scope is eligible for garbage collection.

The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.

Just before a scope is destroyed, a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it a chance to perform any necessary cleanup.

Note that, in AngularJS, there is also a $destroy jQuery event, which can be used to clean up DOM bindings before an element is removed from the DOM.

  • $digest()

Processes all of the watchers of the current scope and its children. Because a watcher's listener can change the model, the $digest() keeps calling the watchers until no more listeners are firing. This means that it is possible to get into an infinite loop. This function will throw 'Maximum iteration limit exceeded.' if the number of iterations exceeds 10.

Usually, you don't call $digest() directly in controllers or in directives. Instead, you should call $apply() (typically from within a directives), which will force a $digest().

If you want to be notified whenever $digest() is called, you can register a watchExpression function with $watch() with no listener.

In unit tests, you may need to call $digest() to simulate the scope life cycle.

Example

  1. var scope = ...;
  2. scope.name = 'misko';
  3. scope.counter = 0;
  4.  
  5. expect(scope.counter).toEqual(0);
  6. scope.$watch('name', function(newValue, oldValue) {
  7. scope.counter = scope.counter + 1;
  8. });
  9. expect(scope.counter).toEqual(0);
  10.  
  11. scope.$digest();
  12. // no variable change
  13. expect(scope.counter).toEqual(0);
  14.  
  15. scope.name = 'adam';
  16. scope.$digest();
  17. expect(scope.counter).toEqual(1);
  • $emit(name, args)

Dispatches an event name upwards through the scope hierarchy notifying the registered ng.$rootScope.Scope#$on listeners.

The event life cycle starts at the scope on which $emit was called. All listeners listening for name event on this scope get notified. Afterwards, the event traverses upwards toward the root scope and calls all registered listeners along the way. The event will stop propagating if one of the listeners cancels it.

Any exception emitted from the listeners will be passed onto the $exceptionHandler service.

Parameters

ParamTypeDetailsnamestring

Event name to emit.

args…*

Optional set of arguments which will be passed onto the event listeners.

Returns

Object

Event object (see ng.$rootScope.Scope#$on).

  • $eval(expression, locals)

Executes the expression on the current scope and returns the result. Any exceptions in the expression are propagated (uncaught). This is useful when evaluating Angular expressions.

Example

  1. var scope = ng.$rootScope.Scope();
  2. scope.a = 1;
  3. scope.b = 2;
  4.  
  5. expect(scope.$eval('a+b')).toEqual(3);
  6. expect(scope.$eval(function(scope){ return scope.a + scope.b; })).toEqual(3);
Parameters

ParamTypeDetailsexpression (optional) stringfunction()

An angular expression to be executed.

  • string: execute using the rules as defined in expression.
  • function(scope): execute the function with the current scope parameter.
    locals (optional) object

Local variables object, useful for overriding values in scope.

Returns

*

The result of evaluating the expression.

  • $evalAsync(expression)

Executes the expression on the current scope at a later point in time.

The $evalAsync makes no guarantees as to when the expression will be executed, only that:

  • it will execute after the function that scheduled the evaluation (preferably before DOM rendering).
  • at least one $digest cycle will be performed after expression execution.
    Any exceptions from the execution of the expression are forwarded to the $exceptionHandler service.

Note: if this function is called outside of a $digest cycle, a new $digest cycle will be scheduled. However, it is encouraged to always call code that changes the model from within an $apply call. That includes code evaluated via $evalAsync.

Parameters

ParamTypeDetailsexpression (optional) stringfunction()

An angular expression to be executed.

  • string: execute using the rules as defined in expression.
  • function(scope): execute the function with the current scope parameter.
  • $new(isolate)

Creates a new child scope.

The parent scope will propagate the $digest() and $digest() events. The scope can be removed from the scope hierarchy using $destroy().

$destroy() must be called on a scope when it is desired for the scope and its child scopes to be permanently detached from the parent and thus stop participating in model change detection and listener notification by invoking.

Parameters

ParamTypeDetailsisolateboolean

If true, then the scope does not prototypically inherit from the parent scope. The scope is isolated, as it can not see parent scope properties. When creating widgets, it is useful for the widget to not accidentally read parent state.

Returns

Object

The newly created child scope.

  • $on(name, listener)

Listens on events of a given type. See $emit for discussion of event life cycle.

The event listener function format is: function(event, args…). The event object passed into the listener has the following attributes:

  • targetScope - {Scope}: the scope on which the event was $emit-ed or $broadcast-ed.
  • currentScope - {Scope}: the current scope which is handling the event.
  • name - {string}: name of the event.
  • stopPropagation - {function=}: calling stopPropagation function will cancel further event propagation (available only for events that were $emit-ed).
  • preventDefault - {function}: calling preventDefault sets defaultPrevented flag to true.
  • defaultPrevented - {boolean}: true if preventDefault was called.
Parameters

ParamTypeDetailsnamestring

Event name to listen on.

listenerfunction(event, args…

Function to call when the event is emitted.

Returns

function()

Returns a deregistration function for this listener.

  • $watch(watchExpression, listener, objectEquality)

Registers a listener callback to be executed whenever the watchExpression changes.

  • The watchExpression is called on every call to $digest() and should return the value that will be watched. (Since $digest() reruns when it detects changes the watchExpression can execute multiple times per $digest() and should be idempotent.)
  • The listener is called only when the value from the current watchExpression and the previous call to watchExpression are not equal (with the exception of the initial run, see below). The inequality is determined according to angular.equals function. To save the value of the object for later comparison, the angular.copy function is used. It also means that watching complex options will have adverse memory and performance implications.
  • The watch listener may change the model, which may trigger other listeners to fire. This is achieved by rerunning the watchers until no changes are detected. The rerun iteration limit is 10 to prevent an infinite loop deadlock.
    If you want to be notified whenever $digest is called, you can register a watchExpression function with no listener. (Since watchExpression can execute multiple times per $digest cycle when a change is detected, be prepared for multiple calls to your listener.)

After a watcher is registered with the scope, the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In rare cases, this is undesirable because the listener is called when the result of watchExpression didn't change. To detect this scenario within the listener fn, you can compare the newVal and oldVal. If these two values are identical (===) then the listener was called due to initialization.

The example below contains an illustration of using a function as your $watch listener

Example

  1. // let's assume that scope was dependency injected as the $rootScope
  2. var scope = $rootScope;
  3. scope.name = 'misko';
  4. scope.counter = 0;
  5.  
  6. expect(scope.counter).toEqual(0);
  7. scope.$watch('name', function(newValue, oldValue) {
  8. scope.counter = scope.counter + 1;
  9. });
  10. expect(scope.counter).toEqual(0);
  11.  
  12. scope.$digest();
  13. // no variable change
  14. expect(scope.counter).toEqual(0);
  15.  
  16. scope.name = 'adam';
  17. scope.$digest();
  18. expect(scope.counter).toEqual(1);
  19.  
  20.  
  21.  
  22. // Using a listener function
  23. var food;
  24. scope.foodCounter = 0;
  25. expect(scope.foodCounter).toEqual(0);
  26. scope.$watch(
  27. // This is the listener function
  28. function() { return food; },
  29. // This is the change handler
  30. function(newValue, oldValue) {
  31. if ( newValue !== oldValue ) {
  32. // Only increment the counter if the value changed
  33. scope.foodCounter = scope.foodCounter + 1;
  34. }
  35. }
  36. );
  37. // No digest has been run so the counter will be zero
  38. expect(scope.foodCounter).toEqual(0);
  39.  
  40. // Run the digest but since food has not changed cout will still be zero
  41. scope.$digest();
  42. expect(scope.foodCounter).toEqual(0);
  43.  
  44. // Update food and run digest. Now the counter will increment
  45. food = 'cheeseburger';
  46. scope.$digest();
  47. expect(scope.foodCounter).toEqual(1);
  48.  
Parameters

ParamTypeDetailswatchExpressionfunction()string

Expression that is evaluated on each $digest cycle. A change in the return value triggers a call to the listener.

Callback called whenever the return value of the watchExpression changes.

  • string: Evaluated as expression
  • function(newValue, oldValue, scope): called with current and previous values as parameters.
    objectEquality (optional) boolean

Compare object for equality rather than for reference.

Returns

function()

Returns a deregistration function for this listener.

  • $watchCollection(obj, listener)

Shallow watches the properties of an object and fires whenever any of the properties change (for arrays, this implies watching the array items; for object maps, this implies watching the properties). If a change is detected, the listener callback is fired.

  • The obj collection is observed via standard $watch operation and is examined on every call to $digest() to see if any items have been added, removed, or moved.
  • The listener is called whenever anything within the obj has changed. Examples include adding, removing, and moving items belonging to an object or array.

Example

  1. $scope.names = ['igor', 'matias', 'misko', 'james'];
  2. $scope.dataCount = 4;
  3.  
  4. $scope.$watchCollection('names', function(newNames, oldNames) {
  5. $scope.dataCount = newNames.length;
  6. });
  7.  
  8. expect($scope.dataCount).toEqual(4);
  9. $scope.$digest();
  10.  
  11. //still at 4 ... no changes
  12. expect($scope.dataCount).toEqual(4);
  13.  
  14. $scope.names.pop();
  15. $scope.$digest();
  16.  
  17. //now there's been a change
  18. expect($scope.dataCount).toEqual(3);
Parameters

ParamTypeDetailsobjstringFunction(scope

Evaluated as expression. The expression value should evaluate to an object or an array which is observed on each $digest cycle. Any shallow change within the collection will trigger a call to the listener.

listenerfunction(newCollection, oldCollection, scope

a callback function that is fired with both the newCollection and oldCollection as parameters. The newCollection object is the newly modified data obtained from the obj expression and the oldCollection object is a copy of the former collection data. The scope refers to the current scope.

Returns

function()

Returns a de-registration function for this listener. When the de-registration function is executed, the internal watch operation is terminated.

Properties

  • $id

Returns

number

Unique scope ID (monotonically increasing alphanumeric sequence) useful for debugging.

Events

  • $destroy

Broadcasted when a scope and its children are being destroyed.

Note that, in AngularJS, there is also a $destroy jQuery event, which can be used to clean up DOM bindings before an element is removed from the DOM.

Type:

broadcast

Target:

scope being destroyed