1

How to use innerHTML from typescript code with Angular 2 RC4? I have this problem: when an user click one specific button I want to add some precompiled html code. For example:

Typescript code

private addHTML() {
    // the html go there I suppose, I don't know how to implement this part
}

HTML code

<div class="form-group row">
<label for="exampleSelect1" class="col-xs-2 col-form-label">My HTML code</label>
<div class="col-xs-10">
    <button type="button" class="btn btn-primary" (click)="addHTML">ADD</button>
</div>
<hr>

Or maybe this is a wrong way. Let me know, thanks in advance.

1 Answer 1

4

Something like this maybe:

htmlYouWantToAdd;

private addHTML() {
    this.htmlYouWantToAdd = "<b>Some HTML you want to display</b>";
}

And your HTML:

<div class="form-group row">
<label for="exampleSelect1" class="col-xs-2 col-form-label">My HTML code</label>
<div class="col-xs-10">
    <button type="button" class="btn btn-primary" (click)="addHTML()">ADD</button>
</div>
<hr>
<div *ngIf="htmlYouWantToAdd" [innerHTML]="htmlYouWantToAdd"></div>
Sign up to request clarification or add additional context in comments.

3 Comments

This method work but only once. If I click a second or third time it doesn't add the html. Do you have any suggestion? Thanks in advance.
@DanielZarioiu What do you mean? I just tested it - created a method that sets htmlYouWantToAdd to null, and after I call addHTML again, it works.
Ok, there was something wrong with my code. Thanks for your solution it work very well!!

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.