Reputation: 3450
I'm developing Angular 8 app and I want to insert HTML with angular bindings into my page.
So, my html code is:
<button (click)="insertHtml()">Insert</button>
<div id="text"></div>
My angular code is:
insertHtml() {
document.getElementById("text").innerHTML = '<span (click)="handleClick(' + this.item.id + ')" [hidden]="' + this.item.hidden + '">'
}
Html looks fine, but it doesn't show right css class and click handler.
Can I insert html with angular bindings dynamically? And if yes, what am I doing wrong?
Upvotes: 0
Views: 272
Reputation: 1629
you can use another approch (something like this):
<button (click)="showHtml()">Insert</button>
<ng-container *ngIf="clicked">
<div id="text"><span (click)="......" [hidden]="' + ...... + '">'</div>
</ng-container>
and in you .ts you do:
@Input()
clicked:boolean =false;
showHtml(){
this.clicked=true;
}
it must work.
Upvotes: 1