一段时间以来,我一直在尝试弄清楚ngAfterContentChecked()和ngAfterViewChecked()的含义。我尝试过各种帖子,但仍然不能理解它们的确切含义。以下是angular.io中给出的定义。有没有人能举个好例子好好解释一下。
ngAfterViewChecked()- Respond after Angular checks the component's views and child views.
Called after the ngAfterViewInit and every subsequent ngAfterContentChecked().
A component-only hook.
ngAfterContentChecked()- Respond after Angular checks the content projected into the component.
Called after the ngAfterContentInit() and every subsequent ngDoCheck().
A component-only hook.发布于 2018-02-08 00:59:55
假设我们有这样的Html
<div> // div A
  <app-my-component>
    <div>
      this is some content for the component app-my-component
    </div>
  </app-my-component>
</div> // div B假设我们有这样的组件
@Component({
  selector: 'app-my-component',
  template: `
    <div> // div C
      here is the View HTML but below we receive content html
      <ng-content></ng-content>
    </div> // div D
  `
})在我们的组件中,从div A一直到div B,这就是View。因此,当我们到达B时,AfterViewChecked将运行。内容是驻留在ng-content标记中的所有内容。因此,当我们到达div D时,AfterContentChecked就会运行。但是,对视图的任何更改都会触发额外的AfterViewCheck,这也应该会触发AfterContentCheck
https://stackoverflow.com/questions/44667135
复制相似问题