首页 > Windows开发 > 详细

[Angular] Difference between ngAfterViewInit and ngAfterContentInit

时间:2017-02-20 01:06:17      阅读:366      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/Answer1215/p/6417872.html

(1)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!