草庐IT

javascript - Angular 2 : How do you render HTML from a JSON response without displaying the tags to the user?

coder 2023-04-29 原文

编辑:澄清一下,我的问题是关于 Angular 2,而不是 1。


我有一个类似这样的组件模板:

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

对象是这样的:

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

而不是像这样渲染段落:

帖子正文

它显示:

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

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

有没有一种简单的方法可以让它发挥作用?有没有一种安全的方法可以让它发挥作用?

最佳答案

在 Angular2 中,您可以使用 property binding在您的情况下访问 DOM 元素的属性:

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

关于javascript - Angular 2 : How do you render HTML from a JSON response without displaying the tags to the user?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34936027/

有关javascript - Angular 2 : How do you render HTML from a JSON response without displaying the tags to the user?的更多相关文章

随机推荐