Angular 2 detectChanges vs setTimeout()

2019-07-28 06:19发布

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?

0条回答
登录 后发表回答