I want to create a button dynamically.I used innerHtml to do this. I can create button.But it's click event not working. Please tell me how to solve this?
Here is my html code
<div [innerHTML]="answerPanelContent"></div>
Here is my typescript code
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('clicked');
}
Here is the stackblitz url: https://stackblitz.com/edit/angular-nka4w9
*ngIf="buttonShown"on the button in the template, and in the TS code setthis.buttonShownto true to make it appear.