I have a custom directive that I'm trying to use to append an element to:
<ng-template #tooltipContent>
<span>hello world</span>
</ng-template>
<button
tooltipText="tooltip text"
[appendedLinkElement]="tooltipContent"
>
</button>
In my tooltip.directive.ts custom directive file I can successfully append text like so:
// ...
@Directive({
selector: '[pslTooltip]',
// ...
})
export class TooltipDirective ... {
@Input('tooltipText') tooltipText: string;
@Input() appendedLinkElement: TemplateRef<any>;
// ...
constructor(private el: ElementRef, private renderer: Renderer2) { }
create() {
this.tooltip = this.renderer.createElement('span');
this.renderer.appendChild(
this.tooltip,
this.renderer.createText(this.tooltipText) // ** this works, but how do I append the TemplateRef?
);
this.renderer.appendChild(document.body, this.tooltip);
// ...
}
}
But when I try to replace the appendChild code from above example with the following, I get an error:
// attempt to append the `#tooltipContent` TemplateRef:
this.renderer.appendChild(
this.tooltip,
this.appendedLinkElement,
);
// I get a console error "TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'."