Angular ngDoCheck() gets called even with ChangeDetectionStrategy.OnPush

前端 未结 1 394
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 02:23

Lets say i have a component structure like this:

AppComponent
    HeaderComponent
    ContentComponent
        TodosComponent
            TodoComponent
         


        
1条回答
  •  离开以前
    2021-01-05 02:56

    Yes, that's the correct behavior. The article If you think ngDoCheck means your component is being checked — read this article explains the behavior in great details. Here is the short version.

    The ngDoCheck is triggered before the component is being checked. This is done to allow you to perform some custom logic and then mark the component for a check. You know that Angular tracks @Inputs by object references but you can use ngDoCheck to make your custom tracking. Here is the simple example:

    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();
          }
       }
    

    Please bear in mind that ngDoCheck is triggered only for the top level component with the strategy OnPush. It's not triggered for this components children.

    Also it is correct that ngAfterViewChecked will be triggered for the component even if now checking was done. This is by design as well.

    I highly recommend you to read Everything you need to know about change detection in Angular, specifically Exploring the implications section. It shows the order of operations you're looking for.

    Also read Why do we need ngDoCheck.

    0 讨论(0)
提交回复
热议问题