如何在 Angular 4+ 中使用 html 标签呈现字符串? [英] How to render string with html tags in Angular 4+?

查看:27
本文介绍了如何在 Angular 4+ 中使用 html 标签呈现字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 angular 4 应用程序中,我有一个类似的字符串

In my angular 4 app, I have a string like

comment: string;
comment = "<p><em><strong>abc</strong></em></p>";

当我在我的 html 中提供此文本时,例如

When I serve this text in my html, like

{{comment}}

然后显示:

<p><em><strong>abc</strong></em></p>

但我需要以粗体和斜体形式显示文本abc",例如abc

But I need to display the text "abc" in bold and italic form, like abc

我该怎么做?

推荐答案

使用单向流语法属性绑定:

<div [innerHTML]="comment"></div>

来自 angular 文档:Angular 将值识别为不安全并自动对其进行消毒,它删除了

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