zoukankan      html  css  js  c++  java
  • [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'.

  • 相关阅读:
    eas之动态刷新Table
    eas之导入导出
    eas之事件
    eas之获得任何一个KDTable的选中行
    eas之创建一个UI界面并对其操作
    eas之style接口
    eas之指定虚模式
    eas之数据融合
    eas之kdtable格式化
    eas之视图冻结与解冻
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6417872.html
Copyright © 2011-2022 走看看