Angular2 change detection “Expression has changed after it was checked”

前端 未结 1 908
执念已碎
执念已碎 2021-01-16 16:34

I have an issue in Angular2 (final) around change detection and data flow between components. I have worked around it but it seems a little hacky to me so was wondering if t

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 17:30

    You can inject private cdRef:ChangeDetectorRef and call this.cdRef.detectChanges() at the end of ngOnChanges(). This way Angular runs change detection again and won't complain about previously modified model values.

    class MyComponent {
      constructor(private cdRef:ChangeDetectorRef) {}
    
      ngOnChanges(changes) {
        this.xxx = ...
        ...
        this.cdRef.detectChanges();
      }
    }
    

    0 讨论(0)
提交回复
热议问题