1

我正在学习角度。有很多生命周期方法。例如 ngOnChanges 每次在子组件的输入属性中有新值时运行 - 这些值是按值传递的。

我不明白这个方法 - ngAfterContentInit。在角度文档中说:

https://angular.io/guide/lifecycle-hooks

在 Angular 将外部内容投射到组件的视图或指令所在的视图中后响应。

但这似乎不是很好的解释。有人可以告诉我为什么我们使用并更好地理解一个真实世界场景 - 用例

4

1 回答 1

1

存在角度内容投影的可能性

// my-container-component.template
<header>
 this is my container
</header>
<main>
   <span> it contains something passed from outside </span>
   <ng-content></ng-content>
</main>

用法:

 <my-container><span>something that will be rendered be INSIDE of the component</span></my-container>

span 将代替ng-content组件内的元素呈现。这段通过的html通常被称为角度的“内容”。ngAfterContentInit()如果您需要,那么与此内容交互是一个很好的钩子

于 2021-02-11T09:58:48.150 回答