Angular的_zone.onMicrotaskEmpty最终会通过changeDetect重新刷新视图
生活随笔
收集整理的這篇文章主要介紹了
Angular的_zone.onMicrotaskEmpty最终会通过changeDetect重新刷新视图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
_zone.onMicrotaskEmpty的觸發起始位置:
class ApplicationRef {/** @internal */constructor(_zone, _console, _injector, _exceptionHandler, _componentFactoryResolver, _initStatus) {this._zone = _zone;this._console = _console;this._injector = _injector;this._exceptionHandler = _exceptionHandler;this._componentFactoryResolver = _componentFactoryResolver;this._initStatus = _initStatus;/** @internal */this._bootstrapListeners = [];this._views = [];this._runningTick = false;this._enforceNoNewChanges = false;this._stable = true;/*** Get a list of component types registered to this application.* This list is populated even before the component is created.*/this.componentTypes = [];/*** Get a list of components registered to this application.*/this.components = [];this._enforceNoNewChanges = isDevMode();this._zone.onMicrotaskEmpty.subscribe({next: () => {this._zone.run(() => {this.tick();});}});tick方法的實現會調用視圖view的detectChange方法:
/*** Invoke this method to explicitly process change detection and its side-effects.** In development mode, `tick()` also performs a second change detection cycle to ensure that no* further changes are detected. If additional changes are picked up during this second cycle,* bindings in the app have side-effects that cannot be resolved in a single change detection* pass.* In this case, Angular throws an error, since an Angular application can only have one change* detection pass during which all change detection must complete.*/tick() {if (this._runningTick) {throw new Error('ApplicationRef.tick is called recursively');}try {this._runningTick = true;for (let view of this._views) {view.detectChanges();}if (this._enforceNoNewChanges) {for (let view of this._views) {view.checkNoChanges();}}}catch (e) {// Attention: Don't rethrow as it could cancel subscriptions to Observables!this._zone.runOutsideAngular(() => this._exceptionHandler.handleError(e));}finally {this._runningTick = false;}}遞歸調用refreshView:
refreshView里執行view template:
從調試器里能看出template所屬的host DOM element,在這個例子里是UnitDetailsComponent內的cx-org-card標簽里第一個div元素:
這里待執行的setter的property和value都已經自動生成了:
這里的highlight似乎有問題,應該是第三行被highlight才對:
instance:FocusDirective
這個__ngSimpleChanges__應該不能被應用程序使用:
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的Angular的_zone.onMicrotaskEmpty最终会通过changeDetect重新刷新视图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明日之后现在抄家掉东西吗
- 下一篇: 末世军团游戏怎么玩(末世危机类小说大全)