angular 2 access ng-content within component

后端 未结 4 1985
半阙折子戏
半阙折子戏 2020-12-01 23:26

How can I access the \"content\" of a component from within the component class itself?

I would like to do something like this:

<         


        
4条回答
  •  天命终不由人
    2020-12-01 23:51

    If you want to get a reference to a component of the transcluded content, you can use:

    @Component({
        selector: 'upper',
        template: ``
    })
    export class UpperComponent {
        @ContentChild(SomeComponent) content: SomeComponent;
    }
    

    If you wrap then you can access access to the transcluded content like

    @Component({
        selector: 'upper',
        template: `
      
    ` }) export class UpperComponent { @ViewChild('contentWrapper') content: ElementRef; ngAfterViewInit() { console.debug(this.content.nativeElement); } }

提交回复
热议问题