Angular 2 detectChanges vs setTimeout()

帅比萌擦擦* 提交于 2019-12-11 07:33:21

问题


I'm facing an issue regarding angular 2 change detection. I'm using a component which uses the 'onPush' strategy and I've noticed that it's child component don't update them self on update (They themselves also 'onPush') so I've added changeDetector.detectChanges() to my code and it caused Maximum call stack size exceeded. I dag up a little bit and wound up doing

setTimeout(() => {
  this.changeDetector.markForCheck();
}, 0);

And the code worked just fine. My question is why angular.detectChanges() cause this error and using setTimeout don't? If I understood currently the setTimeout should run changeDetection also so it pretty much the same, doesn't it?

Is it good practice to use the setTimout like that?

来源:https://stackoverflow.com/questions/43634031/angular-2-detectchanges-vs-settimeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!