如何拥有AngularJS输出转义HTML [英] How to have AngularJS output escaped HTML

查看:666
本文介绍了如何拥有AngularJS输出转义HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获取JSON数据,其中一个字段包含转义的HTML(电子邮件身体竟然):

I am getting JSON data from the server, one of the field contains escaped html (an email body actually):

&放大器; LT; HTML和放大器; GT; \\ r \\ n&放大器; LT;头和放大器; GT; \\ r \\ n&放大器; LT; META HTTP-当量=安培; QUOT;内容类型和放大器; QUOT;内容=安培; QUOT; text / html的;字符集= ISO-8859-1和放大器; QUOT;&放大器; GT; \\ r \\ n&放大器; LT; /头和放大器; GT; \\ r \\ n&放大器; LT;车身DIR =安培; QUOT;汽车和放大器; QUOT;&放大器; GT; \\ r \\ n&放大器; LT; D​​IV&放大器; GT; Buonasera,豪verificato我达蒂SUL MIO帐户ED IL NUMERO双电池车VI豪fornito&放大器; LT; / DIV&放大器; GT; \\ r \\ n&放大器; LT; D​​IV&放大器; GT;&放大器; LT; BR&放大器; GT; \\ r \\ n&放大器; LT; A (更多...)

我在试图用AngularJs以使其变得疯狂。

I am getting crazy at trying to render it with AngularJs.

以下不工作:

<div ng-bind-html-unsafe="mail.htmlbody"></div>

我相信这是正常的,因为HTML实际上是逃过一劫。
我应该先反转义呢?能够与一些现有的服务进行反向转义HTML的角度?

Which I believe is normal because the html is in fact escaped. Should I unescape it first? Is Angular capable of unescaping html with some available service?

如果我使用$ SCE是这样的:

If I use $sce like this:

scope.mail.htmlbody = $sce.trustAsHtml(scope.mail.htmlbody);

显示HTML源文件,检查元素,我可以看到的内容是引用。在页面换句话说源HTML显示,而不是在HTML渲染。也许我失去了一些东西?

The source html is displayed, inspecting the element I can see the content is quoted. In other words in the page the source html is displayed instead of the html being rendered. Maybe I am missing something?

推荐答案

同时引入 $ SCE 服务(角1.2),支持 NG-结合HTML的不安全指令被放弃了。新指令是 NG-绑定-HTML 。如果你使用这一点,code应该工作如记录

At the same time $sce service was introduced (angular 1.2), support for ng-bind-html-unsafe directive was dropped. The new directive is ng-bind-html. If you use this, the code should work as documented:

 <div ng-bind-html="mail.htmlbody"></div>

这篇关于如何拥有AngularJS输出转义HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆