我从服务器获取 JSON 响应,其数据包含带 html 标记的文本。
这会导致问题,因为响应已经包含 html 标签,因此我无法设置页面样式。
有没有一种优雅的方式来处理这个问题?或者我应该通过解析标签来重建响应?
更新
假设 Json 响应有一些名为 instructions
的键:
`instructions` : `"<strong>Please post relevant code</strong>. <p>We have no way to guess how exactly you're trying to use that JSON response</p>"`
现在,当我在我的模板(angularjs)中显示它时,我会说:
<div class="alert alert-info">
{{ response.Instructions }}
</div>
现在,这将在我的模板中显示为 strong
和 p
标签。我想要远程这些模板或者至少能够按原样显示它们。现在我的模板正在使用标签进行渲染,有没有办法将响应中的标签包含在我的标记中?
最佳答案
可以使用ng-bind-html
。需要确保 $santize
模块作为依赖项包含在内。
<div class="alert alert-info" ng-bind-html="response.Instructions">
关于javascript - 清理标记的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216353/