I´m trying to understand the change detector mechanism in Angular 2 applications. I think I made some progress reading several posts :) but not a lot. For what I understand the situation is like this:
When the component is not using the OnPush strategy then it will run the associated change detector in case of:
1) Any part of the app launch a DOM event 2) Any part of the app resolve an HTTP request 3) Any parte execute async code with setTimeout or setInterval
All the components will be checked top to bottom in the component tree.
When the component is using the OnPush strategy then it will run the change detector when:
1) Any of its @Input change its reference (Immutability involved here) 2) The component (or a child) launch an event 3) An Observable launch an event. In this case, if we use Observable as @Input then we have to call the markForCheck() method in the change detector.
OnPush components mark all its subtree of components for not running change detection as well.
The change detection always start at the root component and will go top to bottom the component tree.
So I build a sample application (links at the end of the text) with a tree of components in 3 levels (parent, child and grand-child). The second level components are OnPush components. All the components have click events attached so when click any of them the change detector is executed. When the ngAfterViewChecked hook is executed I change the component color to red for some seconds.
What I can´t understand is why when I click on grand-child components all the child change detectors are running. I thought only the one clicked in the subtree from parent to grand-child will be executed.
When I click the parent is the same. All childs detectors are executed. Why??
Any good person can explain me what is going on?
Thanks!!