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(更多…)

我正疯狂地试图用AngularJ来渲染它.

以下选项不起作用:

<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);

显示源html,判断元素我可以看到内容被引用.换言之,在页面中显示源HTML,而不是呈现HTML.也许我错过了什么?

推荐答案

在引入$sce服务(angular 1.2)的同时,对ng-bind-html-unsafe指令的支持被放弃.新指令是ng-bind-html.如果使用此选项,代码应该可以工作as documented:

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

Json相关问答推荐

Jolt转换,如果任何字段为空,则将对象值设置为空

替换字符串中特殊字符的Jolt变换

对面的行/列进行排序时可能出现错误

Allof Indide的JSON模式之一

使用自定义类型在Golang中解析JSON数组

有没有办法让serde_json正确/不正确地处理NaN、inf和-inf(IEEE 754特殊标准)?

PowerShell:使用JSON原生的Short命令处理JSON?

为什么 Django Rest API 序列化器没有正确序列化多对多字段

Jolt 规范将父对象中的所有键应用于数组中的所有对象

jq可以在两个JSON对象列表中依次添加对象吗?

xidel:是否可以从 JSON 对象中检索特定的嵌套值?

使用 Jolt 变换将平面 json 转换为具有多个数组的嵌套 Json

我无法在 Go - Gin 中解析日期/时间

在 Flutter 中将对象转换为可编码对象失败

如何找出实际安装了哪个版本的 bower 包?

哪个更好:Json 或 XML (PHP)

直接从 Java 中的模型类创建 JSON 对象

Retrofit2.0 得到 MalformedJsonException 而 json 似乎正确?

SCRIPT5009:JSON未定义

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题