javascript - 清理标记的 JSON 响应

标签 javascript html json angularjs

我从服务器获取 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>

现在,这将在我的模板中显示为 strongp 标签。我想要远程这些模板或者至少能够按原样显示它们。现在我的模板正在使用标签进行渲染,有没有办法将响应中的标签包含在我的标记中?

最佳答案

可以使用ng-bind-html。需要确保 $santize 模块作为依赖项包含在内。

<div class="alert alert-info" ng-bind-html="response.Instructions">

See angular ng-bind-html docs

关于javascript - 清理标记的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216353/

相关文章:

javascript - 将字符串格式化为 JSON 对象以在 HTML 中显示在带有/[(ngModel)] 的文本区域中

javascript - 在javascript中动态解析json对象

javascript - e.preventDefault();不适用于导入的自定义按钮组件 React js

asp.net - jquery 脚本删除

jquery - HTML 5 文件加载图像作为背景图像

html - 设置最小高度等于内容高度

javascript位置搜索url中的多个查询参数

javascript - 从 fetch 请求中提取数据并将其导出

html - 3 列到 2 列。多行

java - 将 json URL 导入 java 并使用 jackson 库解析它