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

查看:162
本文介绍了如何在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识别出该值是不安全的,并自动对其进行了清理,会删除<script>标记,但保留诸如<b>元素之类的安全内容."

From angular docs: "Angular recognizes the value as unsafe and automatically sanitizes it, which removes the <script> tag but keeps safe content such as the <b> element."

这篇关于如何在Angular 4+中使用html标签呈现字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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