I'm trying to pass a ng-template
to my directive, something like this:
component.html
<div myDirective [myTemplate]="myTemplate"></div>
<ng-template #myTemplate><p>Lorem ipsum dolor sit amet.</p></ng-template>
directive.ts
import {Directive, Input} from "@angular/core"
@Directive({
selector: "[myDirective]"
})
export class MyDirective {
@Input() myTemplate
ngOnInit () {
console.log(this.myTemplate.elementRef.nativeElement)
}
}
But it just shows <!-- -->
. How do I access the contents of the template? None of the other properties seem to have it - comments on this question suggest using templateRef
instead, but it's undefined.
Stackblitz if needed.
To clarify, the ultimate goal of the directive is to conditionally add the contents of the template to its parent element (along with some other changes).