微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular – 从ng-content访问@ViewChild模板

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

app组件:

<table-column>
  <template #template let-item="item">
    <input type="text" [(ngModel)]="item.foo" />
  </template>
</table-column>

表列组件:

@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)来定义问题…

如果你想在Light DOM中搜索,那么你需要使用@ContentChild并等到ngAfterContentinit挂钩
@ContentChild('template') template;  

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

也可以看看

> What’s the difference between @ViewChild and @ContentChild?

原文地址:https://www.jb51.cc/angularjs/142193.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐