首页 > 代码库 > [Angular] Difference between ngAfterViewInit and ngAfterContentInit
[Angular] Difference between ngAfterViewInit and ngAfterContentInit
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] Difference between ngAfterViewInit and ngAfterContentInit
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。