Is there a way to dynamically create a button in Angular using innerHtml? I have managed to create the button, but unfortunately, its click event is not functioning properly. Can anyone provide guidance on how to resolve this issue?
Below is my HTML code snippet:
<div [innerHTML]="answerPanelContent"></div>
And here is my TypeScript code snippet:
answerPanelContent: any;
constructor(private sanitizer: DomSanitizer){
}
ngOnInit() {
this.answerPanelContent = this.sanitizer.bypassSecurityTrustHtml(`<button type="button" class="btn btn-primary float-left"
(click)="removeAnswer()" title="Remove Answer"
aria-label="Close">
Remove</button>`);
}
removeAnswer(){
alert('Button clicked!');
}
If you'd like to view a working example, check out the StackBlitz URL provided: https://stackblitz.com/edit/angular-nka4w9