1. 程式人生 > >ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous v

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous v

在constructor中匯入ChangeDetectorRef元件,通過手動控制檢查機制的開始和結束

import { Component, ChangeDetectorRef } from 'angular2/core'

@Component({
  selector: 'my-app',
  template: `<div>I'm {{message}} </div>`,
})
export class App {
  message: string = 'loading :(';

  constructor(cdr: ChangeDetectorRef) {
    this.cdr = cdr;
  }

  ngAfterViewInit() {
    
this.cdr.detach(); // 停止檢測
let componentRef = viewContainerRef.createComponent(componentFactory);
(<ModalBodyComponent>componentRef.instance).data = this.config.data;
setTimeout(() => this.cdr.reattach()); // 待元件動態載入完之後重新 attach
}}