ngDoCheck
I have used OnPush strategy for my component, but the ngDoCheck lifecycle hook is still triggered. Is the strategy not working?
1、触发时机:
ngDoCheck 非顾名思义,不是“执行变更检查”时trigger,而是在update binding后就触发,即在ngOnChanges后。
举个栗子::A->B->C

2、为什么这样设计?
You probably know that Angular tracks binding inputs by object reference. It means that if an object reference hasn’t changed the binding change is not detected and change detection is not executed for a component that uses OnPush strategy.
如果,binding的object reference引用不变,即使某一个属性变了,也不会触发。Angular的这个设计,是基于推荐用immutable的思想(例如通过store返回new state这样)。
举个栗子: A->B


在 A 中被更新的o.name,由于input bindings——o的引用没变(即不是新object),并不会触发 B 的变更检查。
但如果你真的有这样的需求场景:一定要用 mutable data 来触发 B 变化
那么(除了在 A 中 用detechChanges来触发 A 及 A 以下所有组件更新?——这么不优雅又业务分散的办法以外)
Angular提供了ngDoCheck,来提前改变子组件的 changeDetectorState。
对于本个栗子就是:在 “判断B是否需要变化” 返回false,即将跳过 B 的变更检查之前,允许你干涉 “判断B是否需要变化” 这件事。
