Angular2 innerHtml删除样式

时间:2016-09-30 15:03:30

标签: angular typescript angular2-template

我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我像这样打印: {{ poi.content }}

它给了我正确的内容: `

<table border="0" cellpadding="5" cellspacing="0">
   <tbody>
     <tr>
       <td style="vertical-align: top;">Tes11t</td>
       <td style="width: 2px;">&nbsp;</td>
       <td style="width: 1px;"><img alt="midgetgolf-sport-faciliteiten-vakantiepark-2.jpg" src="http://beeksebergen.dev/app_dev.php/media/cache/resolve/full_size/midgetgolf-sport-faciliteiten-vakantiepark-2.jpg" style="height: 67px; width: 100px;" /></td>
      </tr>
    </tbody>
 </table>

`

但是当我使用[innerHtml]="poi.content"时,它会给我这个html:

<table border="0" cellpadding="5" cellspacing="0">
    <tbody>
        <tr>
            <td>Tes11t</td>
            <td>&nbsp;</td>
            <td><img alt="midgetgolf-sport-faciliteiten-vakantiepark-2.jpg" src="http://beeksebergen.dev/app_dev.php/media/cache/resolve/full_size/midgetgolf-sport-faciliteiten-vakantiepark-2.jpg"></td>
        </tr>
    </tbody>
</table>

当我使用[innerHtml]

时,是否有人知道为什么它会剥离我的样式

1 个答案:

答案 0 :(得分:17)

Angular2清理动态添加的HTML,样式......

@Pipe({name: 'safeHtml'})
export class Safe {
  constructor(private sanitizer:DomSanitizer){}

  transform(html) {
    return this.sanitizer.bypassSecurityTrustHtml(html);
  }
}
[innerHtml]="poi.content | safeHtml"