角度4的HTML编码 [英] Html encoding in angular 4

查看:42
本文介绍了角度4的HTML编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在角度4的html编码方面,我需要您的帮助.我在数据库中有一些产品记录,其中fullDescription字段采用以下格式:

I need your help with html encoding in angular 4. I have some product records in database, with fullDescription field in this format:

<div align="justify"><span>

使用< div * ngIf ="product" [innerHTML] ="product.fullDescription"></div> ,我得到以下输出:

Using <div *ngIf="product" [innerHTML]="product.fullDescription"></div> I'm getting the following output:

<div align="justify"><span>

为文本.是否可以将其呈现为innerHtml而不是文本?

as text. Is this possible to render this as the innerHtml and not as text?

谢谢.

推荐答案

在组件的类中,您可以使用HTML实体解码器功能,如下所示:

In your component's class, you can use HTML entity decoder function like this:

toHTML(input) : any {
    return new DOMParser().parseFromString(input, "text/html").documentElement.textContent;
}

,并在模板中使用:

<div *ngIf="product" [innerHTML]="toHTML(product.fullDescription)"></div>

这篇关于角度4的HTML编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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