【问题标题】:How to query child component injected by *ngTemplateOutlet如何查询 *ngTemplateOutlet 注入的子组件
【发布时间】:2019-05-20 23:52:44
【问题描述】:

查询模板出口注入的模板似乎不起作用,下面是一个示例,我将子组件作为模板输入注入父组件,但无法使用 @ViewChild 查询该组件..

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

【问题讨论】:

  • 你为什么要那样做?使用输入属性绑定 ref
  • @chellappan 组件重用,我正在制作滑块,你能推荐我准备使用包吗?
  • 你要我建议吗?
  • @Chellappan 是的,手动制作花费的时间比我预期的要多

标签: angular


【解决方案1】:

需要从app组件中访问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)
    }
}

【讨论】:

  • 问题状态是查询位于 ngtemplate 中的子节点
  • 不确定我们是否可以通过TemplateRef访问该元素
猜你喜欢
  • 2017-05-15
  • 2016-04-05
  • 2017-05-23
  • 2021-04-18
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
相关资源
最近更新 更多