How to check if a detection change has been detected on a component

In my application, I want to set manual change detection. To do this, I set ChangeDetectionStrategry to OnPush, and whenever a change in a component occurs, I manually trigger change detection using detectChanges.

If I set ChangeDetectionStrategy to OnPush on the parent component, as I understand it, it will trigger change detection only once on the parent component and only once on the child component, even if I do not set ChangeDetectionStrategy to OnPush on the child. If there is any change in the parent component, I run detectChanges () on the parent component. And if there are any changes in the child component, I run detectChanges () on the child component.

Please suggest it right? or is there a better way?

Secondly, is there a way to check whether its work is working as expected and if a change in a specific component is not detected.

+4
source share
2 answers

Since ChangeDetectionStrategy.OnPushchange detection is performed in the child component, when the value of the child component has been updated @Input(), the event received by the child component has been received, (someEvent)="..."or the event has been selected @HostListener(...)to use the observed border to use the tube | async.

To run the code when changing @Input(), you can either enter setter or implement it OnChangesfor the code that will be executed during updates.

For events, just call your code in the event handler.

For observables, you can apply a type operator .map(...)to your code, which will execute when the values ​​are emitted.

+4
source

"" , ChangeDetectorRef.detach(), OnPush. , , , , / , (.. ), (.. ).

, , , OnPush, , , . @ OnPush .

,

, ngDoCheck() console.log(). /.

+5

Source: https://habr.com/ru/post/1655732/


All Articles