An injectable service for executing work inside or outside of the Angular zone.

The most common use of this service is to optimize performance when starting a work consisting of one or more asynchronous tasks that don't require UI updates or error handling to be handled by Angular. Such tasks can be kicked off via #runOutsideAngular and if needed, these tasks can reenter the Angular zone via #run.

<!-- TODO: add/fix links to: - docs explaining zones and the use of zones in Angular and change-detection - link to runOutsideAngular/run (throughout this file!) -->

Example (live demo)

import {Component, View, NgZone} from 'angular2/core';
import {NgIf} from 'angular2/common';
 
@Component({
  selector: 'ng-zone-demo'.
  template: `
    <h2>Demo: NgZone</h2>
 
    <p>Progress: {{progress}}%</p>
    <p *ngIf="progress >= 100">Done processing {{label}} of Angular zone!</p>
 
    <button (click)="processWithinAngularZone()">Process within Angular zone</button>
    <button (click)="processOutsideOfAngularZone()">Process outside of Angular zone</button>
  `,
  directives: [NgIf]
})
export class NgZoneDemo {
  progress: number = 0;
  label: string;
 
  constructor(private _ngZone: NgZone) {}
 
  // Loop inside the Angular zone
  // so the UI DOES refresh after each setTimeout cycle
  processWithinAngularZone() {
    this.label = 'inside';
    this.progress = 0;
    this._increaseProgress(() => console.log('Inside Done!'));
  }
 
  // Loop outside of the Angular zone
  // so the UI DOES NOT refresh after each setTimeout cycle
  processOutsideOfAngularZone() {
    this.label = 'outside';
    this.progress = 0;
    this._ngZone.runOutsideAngular(() => {
      this._increaseProgress(() => {
      // reenter the Angular zone and display done
      this._ngZone.run(() => {console.log('Outside Done!') });
    }}));
  }
 
 
  _increaseProgress(doneCallback: () => void) {
    this.progress += 1;
    console.log(`Current progress: ${this.progress}%`);
 
    if (this.progress < 100) {
      window.setTimeout(() => this._increaseProgress(doneCallback)), 10)
    } else {
      doneCallback();
    }
  }
}
Implemented by

Static Methods

assertInAngularZone() → void

assertNotInAngularZone() → void

isInAngularZone() → bool

Constructors

NgZone({enableLongStackTrace: false })

enabled in development mode as they significantly impact perf.

Properties

hashCode → int

Get a hash code for this object.

read-only, inherited
hasPendingMacrotasks → bool

Whether there are any outstanding microtasks.

read-only
hasPendingMicrotasks → bool

Whether there are any outstanding microtasks.

read-only
onError EventEmitter

Notify that an error has been delivered.

read-only
onMicrotaskEmpty EventEmitter

Notifies when there is no more microtasks enqueue in the current VM Turn. This is a hint for Angular to do change detection, which may enqueue more microtasks. For this reason this event can fire multiple times per VM Turn.

read-only
onStable EventEmitter

Notifies when the last onMicrotaskEmpty has run and there are no more microtasks, which implies we are about to relinquish VM turn. This event gets called just once.

read-only
onUnstable EventEmitter

Notifies when code enters Angular Zone. This gets fired first on VM Turn.

read-only
runtimeType → Type

A representation of the runtime type of the object.

read-only, inherited

Operators

operator ==(other) → bool

The equality operator.

inherited

Methods

noSuchMethod(Invocation invocation) → dynamic

Invoked when a non-existent method or property is accessed.

inherited
run(dynamic fn()) → dynamic

Executes the fn function synchronously within the Angular zone and returns value returned by the function.

runGuarded(dynamic fn()) → dynamic

Same as #run, except that synchronous errors are caught and forwarded via onError and not rethrown.

runOutsideAngular(dynamic fn()) → dynamic

Executes the fn function synchronously in Angular's parent zone and returns value returned by the function.

toString() → String

Returns a string representation of this object.

inherited