首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ngAfterContentChecked()无法理解+ angular 2

ngAfterContentChecked()无法理解+ angular 2
EN

Stack Overflow用户
提问于 2017-06-21 12:56:18
回答 1查看 2.3K关注 0票数 12

一段时间以来,我一直在尝试弄清楚ngAfterContentChecked()ngAfterViewChecked()的含义。我尝试过各种帖子,但仍然不能理解它们的确切含义。以下是angular.io中给出的定义。有没有人能举个好例子好好解释一下。

代码语言:javascript
运行
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2018-02-08 00:59:55

假设我们有这样的Html

代码语言:javascript
运行
复制
<div> // div A
  <app-my-component>
    <div>
      this is some content for the component app-my-component
    </div>
  </app-my-component>
</div> // div B

假设我们有这样的组件

代码语言:javascript
运行
复制
@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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44667135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档