0

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?

4
  • You can check stackoverflow.com/questions/43682801/… or stackoverflow.com/questions/48913344/… Commented Nov 19, 2019 at 15:41
  • Here they show how to create components dynamically Commented Nov 19, 2019 at 15:41
  • @A. Gladkiy my answer is it work? Commented Nov 20, 2019 at 13:41
  • @Doflamingo19 yes, it works, but unfortunately I need to insert my html into table row and here it didn't help me. Commented Nov 20, 2019 at 13:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.