如何让 AngularJS 输出转义 HTML [英] How to have AngularJS output escaped HTML
问题描述
我从服务器获取 JSON 数据,其中一个字段包含转义的 html(实际上是电子邮件正文):
<html>\r\n<head>\r\n<meta http-equiv="Content-Type"内容="文本/html;charset=iso-8859-1">\r\n</head>\r\n<body dir="auto>\r\n<div>Buonasera, ho verificato i dati sul mio account ed il numero di cell che vi ho fornito</div>\r\n<div><br>\r\n<a
(更多...)
我越来越想用 AngularJs 渲染它了.
以下不起作用:
我认为这是正常的,因为实际上 html 已被转义.我应该先逃避它吗?Angular 是否能够使用某些可用服务对 html 进行转义?
如果我像这样使用 $sce:
scope.mail.htmlbody = $sce.trustAsHtml(scope.mail.htmlbody);
显示源html,检查元素我可以看到引用的内容.换句话说,在页面中显示源 html 而不是呈现的 html.也许我错过了什么?
同时引入 $sce
服务(angular 1.2),支持 ng-bind-html-unsafe
指令被删除.新指令是 ng-bind-html
.如果您使用它,代码应该可以如文档所述:
I am getting JSON data from the server, one of the field contains escaped html (an email body actually):
<html>\r\n<head>\r\n<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\r\n</head>\r\n<body dir="auto">\r\n<div>Buonasera, ho verificato i dati sul mio account ed il numero di cell che vi ho fornito</div>\r\n<div><br>\r\n<a
(more...)
I am getting crazy at trying to render it with AngularJs.
The following is not working:
<div ng-bind-html-unsafe="mail.htmlbody"></div>
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?
If I use $sce like this:
scope.mail.htmlbody = $sce.trustAsHtml(scope.mail.htmlbody);
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?
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屋!