如何在Angular 2的typescript代码中使用innerHTML

时间:2016-09-17 22:38:05

标签: angular typescript

如何使用Angular 2 RC4的typescript代码中的innerHTML? 我有这个问题:当用户单击一个特定按钮时,我想添加一些预编译的html代码。例如:

打字稿代码

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

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>

或者这可能是错误的方式。请提前告知我们。

1 个答案:

答案 0 :(得分:2)

这样的事情可能是:

htmlYouWantToAdd;

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

你的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>
相关问题