NgZone
一种用于在 Angular Zone 内部或外部执行任务的可注入服务。
An injectable service for executing work inside or outside of the Angular zone.
class NgZone {
static isInAngularZone(): boolean
static assertInAngularZone(): void
static assertNotInAngularZone(): void
constructor(__0)
hasPendingMacrotasks: boolean
hasPendingMicrotasks: boolean
isStable: boolean
onUnstable: EventEmitter<any>
onMicrotaskEmpty: EventEmitter<any>
onStable: EventEmitter<any>
onError: EventEmitter<any>
run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T
runTask<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], name?: string): T
runGuarded<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T
runOutsideAngular<T>(fn: (...args: any[]) => T): T
}
Provided in
说明
此服务最常见的用途是在启动包含一个或多个不需要 Angular 处理的 UI 更新或错误处理的异步任务的工作时优化性能。可以通过 runOutsideAngular 启动此类任务,如果需要,这些任务可以通过 run 重新进入 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.
Further information available in the Usage Notes...
静态方法
构造函数
属性
属性 | 说明 |
---|---|
hasPendingMacrotasks: boolean | 只读 |
hasPendingMicrotasks: boolean | 只读 |
isStable: boolean | 只读 是否没有未解决的微任务或宏任务。 Whether there are no outstanding microtasks or macrotasks. |
onUnstable: EventEmitter<any> | 只读 在代码进入 “Angular Zone ” 时通知。这首先在 VM 周期中触发。 Notifies when code enters Angular Zone. This gets fired first on VM Turn. |
onMicrotaskEmpty: EventEmitter<any> | 只读 在当前的 VM Turn 中没有更多微任务排队时通知。这是 Angular 进行变更检测的提示,它可能会排队更多的微任务。因此,此事件可在每次 VM 周期中触发多次。 Notifies when there is no more microtasks enqueued 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. |
onStable: EventEmitter<any> | 只读 在最后一个 Notifies when the last |
onError: EventEmitter<any> | 只读 通知已传递的错误。 Notifies that an error has been delivered. |
方法
在 Angular Zone 内同步执行的 Executes the |
通过 Running functions via 在此功能内计划的任何将来的任务或微任务将在 Angular Zone 内继续执行。 Any future tasks or microtasks scheduled from within this function will continue executing from within the Angular zone. 如果发生同步错误,它将被重新抛出,并且不会通过 If a synchronous error happens it will be rethrown and not reported via |
作为任务在 Angular Zone 中同步执行 Executes the |
通过 Running functions via 在此功能内计划的任何将来的任务或微任务都将在 Angular Zone 内继续执行。 Any future tasks or microtasks scheduled from within this function will continue executing from within the Angular zone. 如果发生同步错误,它将被重新抛出,并且不会通过 If a synchronous error happens it will be rethrown and not reported via |
与 Same as |
在 Angular 的父 Zone 中同步执行 Executes the |
通过 runOutsideAngular 运行函数可让你离开 Angular 的 Zone 并执行不会触发 Angular 变更检测或受 Angular 错误处理控制的工作。 Running functions via runOutsideAngular allows you to escape Angular's zone and do work that doesn't trigger Angular change-detection or is subject to Angular's error handling. 从此函数中计划的任何将来的任务或微任务将在 Angular Zone 之外继续执行。 Any future tasks or microtasks scheduled from within this function will continue executing from outside of the Angular zone. 使用 run 重新进入 Angular Zone 并执行更新应用程序模型的工作。 Use run to reenter the Angular zone and do work that updates the application model. |
使用说明
例子
Example
import {Component, NgZone} from '@angular/core';
import {NgIf} from '@angular/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>
`,
})
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();
}
}
}