I'm using a directive to inject HTML dynamically to other HTML elements, specifically to other components in which i can't modify the code. My issue is that if i inject HTML elements in which they call a local function from the same component they don't work. An example of this:
<button class="button-default" (click)="showTable()">Show table info</button>
In this case, doesn't happen nothing if you press the button, the function is not called.
The directive code:
import { Directive, ElementRef,AfterViewInit, Input} from '@angular/core';
@Directive({
selector: '[insertHtml]'
})
export class InsertHtml {
@Input() targetHtml : string;
@Input() html : string;
@Input() position: any;
private el: HTMLInputElement;
constructor(private elementRef: ElementRef) {
this.el = this.elementRef.nativeElement;
}
ngAfterViewInit() {
let target = this.el.querySelector(this.targetHtml );
target.insertAdjacentHTML(this.position, this.html);
}
}
How it works the directive:
<div class="block" [insertHtml]="'ul.nav.nav-tabs'" [html]="htmlButtons" [position]="'afterend'">
htmlButtons would be code i put in the example case.
How i can make the function inside of the injected HTML elements work?