Angular 2:如何在不向用户显示标签的情况下从JSON响应中呈现HTML? [英] Angular 2: How do you render HTML from a JSON response without displaying the tags to the user?

查看:98
本文介绍了Angular 2:如何在不向用户显示标签的情况下从JSON响应中呈现HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改:对于只浏览过标题的人的澄清,我的问题是关于 Angular 2 ,而不是1。

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:

帖子正文

它显示:

< p>< em>帖子正文< / em>。< / p>

由于它是如此常见的任务,我寻找了一个内置的管道,如 {{post.body |安全}} 但没有看到。

Since it's such a common task, I looked for a built-in pipe like {{ post.body | safe }} but didn't see one.

是否有一种简单的方法可以实现这一点?有没有一种安全的方法可以使它工作?

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

推荐答案

在Angular2中你可以使用属性绑定来访问DOM元素的属性:

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

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

这篇关于Angular 2:如何在不向用户显示标签的情况下从JSON响应中呈现HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆