如何在 Angular 4+ 中使用 html 标签呈现字符串? [英] How to render string with html tags in Angular 4+?
本文介绍了如何在 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 将值识别为不安全并自动对其进行消毒,它删除了 标签,但保留了安全内容,例如
元素."
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屋!
查看全文