如何允许在我的 div 中显示 HTML |角度 7.



>我有文本编辑器表单"ngx-editor": "^4.1.0"。例如,在键入的结果中,我有这个。

<div style="text-align: center;"><span style="background-color: transparent;">awd d awd awd</span></div><div style="text-align: center;"><span style="background-color: transparent;"><br></span></div><div style="text-align: left;"><span style="background-color: transparent;">ewfewfsefsefe</span></div>

但是当我想显示编辑器的结果时,我使用基本{{mytext}}但是我有我保存的html,它是由文本编辑器生成的,例如文本 http://prntscr.com/p337qz

[innerHTML] 只显示没有 html 元素的文本。

我如何使用所有 html 样式和元素显示它?

您可以使用bypassSecurityTrustHtml

import { DomSanitizer } from '@angular/platform-browser'
@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform  {
constructor(private sanitized: DomSanitizer) {}
transform(value) {
return this.sanitized.bypassSecurityTrustHtml(value);
}
}

之后设置你的 html

<div [innerHtml]="myText | safeHtml"></div>

请检查示例

最新更新