1

It's clear that we need to use markForCheck() mostly with OnPush strategy to mark component to do the CD check, but

I'm trying to figure out why to check only one branch:

enter image description here

Why could run CD for whole app or only specific component, no?

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
  • Why should we run change detection cycle for other branches if we are trying to improve performance? We open path for checking to component where changes happened – yurzui Jul 30 '17 at 19:50
  • Only the Change Detector Ref for that component was marked for check, since each component's Change Detector Ref is independent of others – Toxicable Jul 30 '17 at 21:37
  • 2
    @Toxicable Look at `markParentViewsForCheck` function that is executed during `markForCheck` https://github.com/angular/angular/blob/4.3.x/packages/core/src/view/util.ts#L110-L118 Are you sure that only cdRef for that component will be marked for check? It will mark for check current component view and all its parents with `OnPush` stategy – yurzui Jul 31 '17 at 04:16
  • @Toxicable, it's not independent, it will run CD for all children of the component – Max Koretskyi Jul 31 '17 at 04:52
  • `markForCheck` doesn't run change detection, see [this answer](https://stackoverflow.com/a/45396740/2545680) – Max Koretskyi Jul 31 '17 at 04:53
  • @yurzui ok, let me refrase: why do we need to mark the whole branch? not only the component itself? – Stepan Suvorov Jul 31 '17 at 06:29
  • @yurzui Sorry I think I mis-worded that statement. I mean siblings are independent of each other. When I wrote that it was with the assumption that it was inferred that the parents leading to the root will also be marked for check. – Toxicable Jul 31 '17 at 08:46
  • @Maximus Where does it say it will run CD for all children? that makes no sense for it to do so. My understanding is that it will mark a path to the root which has nothing to do with any Children components. – Toxicable Jul 31 '17 at 08:46
  • @StepanSuvorov CD is run top to bottom always, CD cannot be run in one place it has to be run from the root down, it'll violate a bunch of rules otherwise, most notably one way data flow, which is top to bottom – Toxicable Jul 31 '17 at 08:47
  • @Toxicable, `detectChanges` runs change detection for the component and for the children. `markForCheck` doesn't run change detection – Max Koretskyi Jul 31 '17 at 08:49
  • @Maximus yes I know, I was never in disagreement in the differance between `detectChanges` and `markForCheck`. However saying it'll run on all children is incorrect, it will only run on the CD Refs marked to be checked if they're OnPush. Otherwise what is the point of OnPush if it's just ignored? – Toxicable Jul 31 '17 at 08:52
  • @Toxicable, yes, sure, detached views (CDs) are not checked. – Max Koretskyi Jul 31 '17 at 08:55

1 Answers1

2

thanks to comments from @yurzui @Toxicable @Maximus I came to understanding that:

Why not on each component?

That was the idea about onPush Strategy optimization: to run it only if it's necessary.

Why the whole branch, but not only one component?

Because it's how CD is made - it always goes from root component down. So if you want to run CD on specific component you need to make all the path from root to this specific component

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176