如何查询由* ngTemplateOutlet

时间:2018-12-20 12:45:20

标签: angular

查询由模板插座注入的模板似乎不起作用,下面是一个示例,我将子组件作为模板输入注入到父组件中,但是无法使用@ViewChild来查询该组件。

https://stackblitz.com/edit/angular-pjqkbn

1 个答案:

答案 0 :(得分:0)

需要从应用程序组件访问ref元素并将其向下传递给父组件

Demo

<app-parent [childTpl]="childElement"></app-parent>
<app-child #child></app-child> 

export class AppComponent  { 
  @ViewChild('child', { read: ElementRef }) childElement: ElementRef;

   ngOnInit(){
      console.log(this.childElement.nativeElement.innerHTML)
    }
}