如何让AngularJS输出转义HTML

Max*_*lli 38 javascript json html-escape-characters angularjs

我从服务器获取JSON数据,其中一个字段包含转义的html(实际上是一个电子邮件正文):

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

我正试图用AngularJs渲染它.

以下不起作用:

<div ng-bind-html-unsafe="mail.htmlbody"></div>
Run Code Online (Sandbox Code Playgroud)

我认为这是正常的,因为html实际上已被转义.我应该先取消它吗?Angular能否通过一些可用的服务来解决html问题?

如果我像这样使用$ sce:

scope.mail.htmlbody = $sce.trustAsHtml(scope.mail.htmlbody);
Run Code Online (Sandbox Code Playgroud)

显示源html,检查我可以看到内容被引用的元素.换句话说,在页面中显示源html而不是正在呈现的html.也许我错过了什么?

Dav*_*yon 46

$sce引入服务的同时(角度1.2),对ng-bind-html-unsafe指令的支持被删除.新指令是ng-bind-html.如果您使用此代码,代码应按照记录的方式工作:

 <div ng-bind-html="mail.htmlbody"></div>
Run Code Online (Sandbox Code Playgroud)

  • 如果我不希望内部 html 的其余部分不被覆盖并且无法为自定义文本创建 span 标记怎么办 (2认同)

Md.*_*min 16

使用此指令:

<div ng-bind-html="mail.htmlbody"></div>
Run Code Online (Sandbox Code Playgroud)

不要忘记在app模块上使用angular sanitize.

点击这里:http://docs.angularjs.org/api/ngSanitize

  • 你能详细说说吗? (2认同)