You can see full proof of concept here: https://plnkr.co/edit/slshjP?p=preview
I want to create a simple tree component, that allows the user to specify template for each node eg.
<app-tree-editor [nodes]="fields">
<ng-template treeTemplate let-node>{{node}}</ng-template>
</app-tree-editor>
I define a directive to get hold of the template:
@Directive({
selector: '[treeTemplate]',
})
export class TreeEditorTemplate {
constructor(public template: TemplateRef<{}>) {
}
}
And then inside of my tree I get a reference to it.
@Component({
selector: 'app-tree-editor',
template: `
<app-tree-node [node]="root" [templateRef]="nodeTemplateRef">
</app-tree-node>
`,
})
export class TreeEditorComponent {
@Input() nodes: any;
@ContentChild(TreeEditorTemplate) nodeTemplate: TreeEditorTemplate;
nodeTemplateRef: TemplateRef<{}>;
root: any;
ngOnInit() {
this.root = {children: this.nodes};
}
ngAfterContentInit(): void {
this.nodeTemplateRef = this.nodeTemplate.template;
}
}
The problem is, the template I get is a comment node :/
Any idea what I'm doing wrong?
Code with repro case: You can see full proof of concept here: https://plnkr.co/edit/slshjP?p=preview