简体   繁体   English

在 Angular 中使用 changeDetection.markForCheck() 的正确方法是什么?

[英]What is the right way to use changeDetection.markForCheck() in Angular?

I am using markForCheck to detect changes in my angular component (Which has changeDetection: ChangeDetectionStrategy.OnPush ) and initially, I put the markForCheck at the start of the function and It was working for me then I realized it will make more sense to put that after all the function action is done.我正在使用markForCheck来检测我的角度组件中的变化(其中有changeDetection: ChangeDetectionStrategy.OnPush ),最初,我将markForCheck放在函数的开头, markForCheckmarkForCheck ,然后我意识到放置它会更有意义在所有功能动作完成后。

In both the approach angular was detecting the changes if it is called initially or after the action is done.在这两种方法中,angular 都会检测变化,如果它最初被调用或在动作完成后被调用。

So if someone can justify what will be the right way to use markForCheck?因此,如果有人可以证明使用 markForCheck 的正确方法是什么?

functionName() { // It works at both places
  this.cd.markForCheck();
  //
  .... Some Code that needs markForCheck ....
  //
  this.cd.markForCheck();
}

u need to use this.cdr.markForCheck() after changed some data and component have property changeDetection: ChangeDetectionStrategy.OnPush更改一些数据和组件后需要使用 this.cdr.markForCheck() 属性 changeDetection: ChangeDetectionStrategy.OnPush

becouse in this case component will not reload self html template if some subchild components have OnPush - parent "markForCheck()" will redraw them too.因为在这种情况下,如果某些子子组件具有 OnPush,组件将不会重新加载自 html 模板 - 父“markForCheck()”也会重绘它们。 if changed data display in subcomponent without OnPush u can don't use "markForCheck()"如果在没有 OnPush 的子组件中显示更改的数据,则不能使用“markForCheck()”

in my case compA have mat-table in html, so after reload items i call markForCheck() for redrawing mat-table在我的例子中,compA 在 html 中有 mat-table,所以在重新加载项目后,我调用 markForCheck() 来重绘 mat-table

You use markForCheck() to tell Angular to 'flag' changes that happened outside of the active change detection strategy.您可以使用markForCheck()告诉 Angular“标记”在主动更改检测策略之外发生的更改。

When you are using the default change detection strategy, markForCheck() is not necessary and my guess is that is why it didn't matter where you originally had your markForCheck() call.当您使用默认更改检测策略时, markForCheck()不是必需的,我猜这就是为什么您最初在markForCheck()调用markForCheck()并不重要。

The correct place to call markForCheck() is inside a component with ChangeDetectionStrategy.OnPush after you make changes that won't get picked up by the OnPush change detection.正确的地方打电话markForCheck()是一个组件内ChangeDetectionStrategy.OnPush你做,不会被OnPush变化检测得到回升更改

Here is a more in-depth explanation that you might find helpful, What's the difference between markForCheck() and detectChanges()这是您可能会觉得有用的更深入的解释, markForCheck() 和detectChanges() 之间什么区别

EDIT: I mistakenly said that markForCheck() triggers the change detection and that is incorrect.编辑:我错误地说 markForCheck() 触发了更改检测,这是不正确的。 Thanks @Fatih Ersoy谢谢@Fatih Ersoy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM