首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ng-content访问@ViewChild模板

从ng-content访问@ViewChild模板
EN

Stack Overflow用户
提问于 2017-04-03 22:32:34
回答 1查看 14.8K关注 0票数 18

是否可以将模板与如下所述的ng-content结合使用:

应用组件:

代码语言:javascript
复制
<table-column>
  <template #template let-item="item">
    <input type="text" [(ngModel)]="item.foo" />
  </template>
</table-column>

表-列组件:

代码语言:javascript
复制
@Component({
  selector: 'table-column',
  template: '<ng-content></ng-content>'
})
export class TableColumnComponent implements OnInit {
  @ViewChild('template') template;  

  ngOnInit() {
    console.log(this.template); // undefined
    // create column object with template and different metadata...
  });      
}

Plunker

我使用不同的生命周期钩子(ngOnInit,ngAfterViewInit)获得undefined的问题是...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-03 22:49:15

如果你想在轻量级DOM中进行搜索,那么你需要使用@ContentChild并等待ngAfterContentInit钩子

代码语言:javascript
复制
@ContentChild('template') template;  

ngAfterContentInit() {
  console.log(this.template);
}

另请参阅

票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43187409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档