我试图使用带有html标签的angular打印数据,但它不起作用

时间:2017-10-30 09:54:28

标签: angular ionic-framework

"description": "<p><s><em><strong>Plurar or singular, seconds, minutes, hours, days, weeks, or years, it runs automatically. I've tested it on Laravel version 4.1.24.</strong></em></s></p>",

当我打印时 {{description}}然后打印:

<p><s><em><strong>Plurar or singular, seconds, minutes, hours, days, weeks, or years, it runs automatically. I've tested it on Laravel version 4.1.24.</strong></em></s></p>

我希望这样:

Plurar or singular, seconds, minutes, hours, days, weeks, or years, it runs automatically. I've tested it on Laravel version 4.1.24.

4 个答案:

答案 0 :(得分:1)

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

写这样的东西。

答案 1 :(得分:1)

要渲染html文件,您已使用[innerHTML]属性。

有时,角度会从你正在渲染的html字符串中删除一些属性。

您可以创建一个用于显示html的管道

import { Pipe, PipeTransform } from "@angular/core";
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({ name: 'securehtml' })
export class SecureHtml {
    constructor(private domSanitizer: DomSanitizer) { }
    transform(html) {
        return this.sanitizer.bypassSecurityTrustHtml(html);
    }
}

和html

<div [innerHtml]="htmlstring | securehtml"></div>

答案 2 :(得分:1)

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

在ts文件中将数据放入描述中它将起作用

答案 3 :(得分:0)

我试过这个并且有效:

{{!!description!!}}