I have simplest Angular structural directive:
import { Directive, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({ selector: '[hello]' })
export class HelloDirective {
constructor(
private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) {
this.viewContainer.createEmbeddedView(this.templateRef);
}
}
I use it this way:
<div *hello>Hello Directive</div>
It shows me "Hello Directive" message as expected. Now I want to change the content by wrapping it with some another component:
<my-component>Hello Directive</my-component>
And I want the directive to do it for me. I know that I can use a Component paradigm and create HelloComponent
instead of HelloDirective
and use ng-template
etc with the template defined by template
or templateUrl
property on the @Component
decorator... But is there an approach that could be used with a Directive paradigm to achieve such a result?
<div *hello><my-component>Hello Directive</my-component></div>
at the end (instead of<div *hello>Hello Directive</div>
). – Rumerymy-component
containng-content
? – Donatusmy-component
implementation in this task. – Rumery