Edit: a clarification for anyone who only skimmed the title, my question is about Angular 2, not 1.


I have a component template that is something like this:

<div>{{ post.body }}</div>

该对象类似于:

{
    "title": "Some Title",
    "body": "<p>The <em>post body</em>.</p>"
}

Instead of rendering the paragraph like:

The post body

它显示:

"<p>The <em>post body</em>.</p>"

由于这是一项非常常见的任务,我寻找了一个内置管道,比如{{ post.body | safe }},但没有看到一个.

Is there is an easy way to get that working? Is there a safe way to get that working?

推荐答案

In Angular2 you can use property binding to access properties of DOM elements, in your case:

<div [innerHTML]="post.body"></div>

Json相关问答推荐

为什么terraform不缩小这个策略JSON?'

您可以使用Jolt对JSON执行OR条件吗

写入JSON文件的流

使用 Redis 作为键值存储

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

将 json 转换为 jsonb 安全吗?

在这种情况下我如何实现 UnmarshalJSON 并且只为一个接口字段定义特殊行为?

JSON 字段的多个名称

Serde JSON 反序列化枚举

如何在linux中用jq过滤json数组?

如何使用 Swiftui 判断 JSON 是否缺少键值对

杰克逊 2.0 和 Spring 3.1

JSON 模式验证

使用非美国日期格式时,JsonConvert.DeserializeObject 无法将字符串转换为 DateTime

规范化 JSON 文件

什么 Python 框架用于没有前端的 REST/JSON Web 服务?

MVC JsonResult camelCase 序列化

在android中使用GSON解析带有动态key和value的JSON

如何向 json IAM 策略添加 comments ?

Volley JsonObjectRequest Post 参数不再起作用