我一直试图弄清楚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.
假设我们有这个Html
原文链接:https://www.f2er.com/angularjs/141752.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