码迷,mamicode.com
首页 > Windows程序 > 详细

[Angular] Difference between ngAfterViewInit and ngAfterContentInit

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

标签:span   foo   template   ntc   div   diff   angular   logs   parent   

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

标签:span   foo   template   ntc   div   diff   angular   logs   parent   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!