I am trying to create an angular directive that does two things.
1. change border of the host element
2. append a button at the end of the host element
As of now i am on the first step where i have to set the border of host element.
HTML
<div *myDirective
<h1> some value </h1>
</div>
Directive
export class MyDirective{
constructor(private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef) {
this.templateRef.elementRef.nativeElement.style['border'] = '1px solid red';
this.viewContainer.createEmbeddedView(this.templateRef);
}
}
Now when i add this directive to a div element,I get the following error,
Cannot set property 'border' of undefined
How could i change the style and append another element to the host using structural directive?
[Edit] As most of the answers are suggesting me to create an attribute directive, i just want to post the statement from angular document regarding structural directives.
They shape or reshape the DOM's structure, typically by adding, removing, or manipulating elements.
In that case creating attribute directive to append a button to the host element is not proper. Isn't it?