Angular使用带有转义元素属性的innerHTML

前端之家收集整理的这篇文章主要介绍了Angular使用带有转义元素属性的innerHTML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@
@H_403_0@
嗨我从这样的休息服务器有一个html respose:

<h3>Center Align</h3>\n<p style=\"text-align: center;\">This is a paragraph. It is center aligned. Center is,but nature,a fence sitter. A flip flopper. It has a difficult time making up its mind. It wants to pick a side. Really,it does. It has the best intentions,but it tends to complicate matters more than help. The best you can do is try to win it over and hope for the best. I hear center align does take bribes.</p>

当我使用innerHTML时,渲染为:

<h3>Center Align</h3>
<p>
This is a paragraph. It is center aligned. Center is,but it tends to complicate matters more than help. The best you can do is try to win it over and hope for the best. I hear center align does take bribes.
</p>

缺少< p>的转义属性标签所以我有一个空的< p>而不是< p style =“text-align:center;”>

有没有办法使用其属性呈现此内容

解决方法

使用 DomSanitizer处理您的HTML.默认情况下这是不安全的.

constructor(private s: DomSanitizer) {
  this.text = 'my HTML';
  this.text = s.bypassSecurityTrustHtml(this.text);
}

plunk.

@H_403_0@

猜你在找的Angularjs相关文章