Angular ngDoCheck () is even called with ChangeDetectionStrategy.OnPush

Suppose I have a component structure like this:

AppComponent
    HeaderComponent
    ContentComponent
        TodosComponent
            TodoComponent

If I set the HeaderComponent changeDetectionto ChangeDetectionStrategy.OnPushand change something in TodoComponent, the HeaderComponent lights up anyway ngDoCheck(), ngAfterViewChecked()and ngAfterContentChecked().

What am I missing? Anyway, does ngDoCheck cause? If so, how can I determine if a component has been checked using ChangeDetection?

+2
source share
1 answer

, . , ngDoCheck , - , . .

ngDoCheck , . , , . , Angular @Input , ngDoCheck . :

Component({
   ...,
   changeDetection: ChangeDetectionStrategy.OnPush
})
MyComponent {
   @Input() items;
   prevLength;
   constructor(cd: ChangeDetectorRef) {}

   ngOnInit() {
      this.prevLength = this.items.length;
   }

   ngDoCheck() {
      if (this.items.length !== this.prevLength) {
         this.cd.markForCheck();
      }
   }

, ngDoCheck OnPush. .

, ngAfterViewChecked , . .

, Angular, Exploring the implications. , .

ngDoCheck.

+2

All Articles