是否可以将模板与如下所述的ng-content结合使用:
应用组件:
<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...
});
}我使用不同的生命周期钩子(ngOnInit,ngAfterViewInit)获得undefined的问题是...
发布于 2017-04-03 22:49:15
如果你想在轻量级DOM中进行搜索,那么你需要使用@ContentChild并等待ngAfterContentInit钩子
@ContentChild('template') template;
ngAfterContentInit() {
console.log(this.template);
}另请参阅
https://stackoverflow.com/questions/43187409
复制相似问题