angular 2 [innerHTML]在html内部渲染(不在角度2上工作)

时间:2017-03-31 13:21:59

标签: html angular render innerhtml

我正在使用Angular 2,我尝试将变量action.render中的文本呈现为html代码:

<p *ngIf="action.isHtml">
      <div [innerHTML]="{{action.content}}"></div>                                               
</p>
<p *ngIf="!action.isHtml">
      {{action.content}}
</p>

innerHTML在我的情况下不起作用。 Angular 2还有其他方法吗? (这是我得到的)

这是我的action.content

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>
            </title>
            <style type="text/css">
                .cs2654AE3A{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt}
                .csC8F6D76{color:#000000;background-color:transparent;font-family:Calibri;font-size:11pt;font-weight:normal;font-style:normal;}
            </style>
        </head>
        <body>
            <p class="cs2654AE3A"><span class="csC8F6D76">New claim declaration by HD team 00015371</span></p></body>
    </html>

enter image description here

1 个答案:

答案 0 :(得分:2)

将您的代码更改为此,然后重试:

<p *ngIf="action.isHtml">
      <div [innerHTML]="action.content"></div>                                               
</p>
<p *ngIf="!action.isHtml">
      {{action.content}}
</p>

您应该使用innerHTML="{{action.content}}"[innerHTML]="action.content",而不是[innerHTML]="{{action.content}}"