I tried to pass an click event to a button that is rendered using DomSanitizer. But the bounded method is not getting called when the button is clicked.
component.ts
export class MyComponent {
htmlElement = '<button class="btn btn-primary" (click)="onClick()">Add</button>';
onClick() {
console.log('submitted');
}
}
component.html
<div [innerHtml]="htmlElement | sanitizer"></div>
pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Pipe({
name: 'sanitizer'
})
export class SanitizerPipe implements PipeTransform {
constructor(private _sanitizer: DomSanitizer) {}
transform(v: string): SafeHtml {
return this._sanitizer.bypassSecurityTrustHtml(v);
}
}
button is displayed properly with style. But click event is not getting triggered.
Is there any fix or alternative solution for this?