[Angular] Difference between ngAfterViewInit and ngAfterContentInit
Angular and between Difference
2023-09-14 08:59:18 时间
Content is what is passed as children. View is the template of the current component.
The view is initialized before the content and ngAfterViewInit()
is therefore called before ngAfterContentInit()
.
@Component({ selector: 'parent-cmp', template: '<div #wrapper><ng-content></ng-content></div>' }) class ParentComponent { @ViewChild('wrapper') wrapper:ElementRef; @ContentChild('myContent') content:ElementRef; ngAfterViewInit() { console.log('ngAfterViewInit - wrapper', this.wrapper); console.log('ngAfterViewInit - content', this.content); } ngAfterContentInit() { console.log('ngAfterContentInit - wrapper', this.wrapper); console.log('ngAfterContentInit - content', this.content); } }
<parent-cmp><div #myContent>foo</div></parent-cmp>
If you run this code, the output for ngAfterViewInit - content
should be null
.
So if you want to change the child component's props, you cannot do it in 'ngAfterViewInit', you need to do it in 'ngAfterContentInit'.
相关文章
- [Angular 8] Calculate and Measure Performance budgets with the Angular CLI
- [Angular] Expose Angular Component Logic Using State Reducers
- [Angular] N things you might don't know about Angular Route
- [Angular] New async 'as' syntax and ngIf.. else
- [Angular] Setup automated deployment with Angular, Travis and Firebase
- [Angular-Scaled web] 1. Architecture and file structure
- [Angular] Overlay CDK
- [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
- [Angular] @ViewChild read custom directive and exportAs
- [Angular] Create a ng-true-value and ng-false-value in Angular by controlValueAccessor
- [Angular] FadeIn and FadeOut animation in Angular
- [Angular] Angular Advanced Features - ng-template , ng-container, ngTemplateOutlet
- [Angular] Dynamic component's instance and sorting
- [Angular] Difference between ngAfterViewInit and ngAfterContentInit
- [Angular 2] A Simple Form in Angular 2
- [RxJS + AngularJS] Sync Requests with RxJS and Angular
- [Angular 2] Using Pipes to Filter Data
- 细说angular Form addControl方法
- Angular 应用里 server.ts 文件的 APP_BASE_HREF token 的用法?
- where and when is Angular scope initialized
- Angular forRoot 方法的使用场合介绍
- 关于 Angular 里 module 和 Component 包含粒度的一个讨论
- Angular 里使用 FormControl 的步骤
- Angular html 页面里的井号 #
- Angular CLI builder 学习笔记
- Angular单元测试框架beforeEach和it的执行顺序
- 通过单步调试理解Angular里routerLink指令实际url的生成逻辑