我一直试图弄清楚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.
答案 0 :(得分:1)
假设我们有这个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中的组件中,一直到div B,即View。因此,当我们到达B时,AfterViewChecked将运行。内容是ng-content标签中的所有内容。所以AfterContentChecked将在我们到达div D时运行。但是任何我意味着对视图的任何更改都可以触发一个额外的AfterViewCheck,它也应该触发AfterContentCheck