Accessing @ViewChild template from ng content
Is it possible to use templates in combination with ng-content as described here:
application component:
<table-column>
<template #template let-item="item">
<input type="text" [(ngModel)]="item.foo" />
</template>
</table-column>
table-column component:
@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 ?
Problem: I am getting undefined
using different lifecycle hooks (ngOnInit, ngAfterViewInit) ...
+3
Ievgen Martynov
source
to share
1 answer
If you want to do a search in Light DOM you need to use @ContentChild
and wait ngAfterContentInit
hook
@ContentChild('template') template;
ngAfterContentInit() {
console.log(this.template);
});
see also
- What is the difference between @ViewChild and @ContentChild?
+6
yurzui
source
to share