新建一个管道文件
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: "html"
})
export class HtmlPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {
}
public transform(style) {
return this.sanitizer.bypassSecurityTrustHtml(style);
}
}
在需要的module下引入
import { HtmlPipe } from '../pipe';
declarations: [
iComponent,
HtmlPipe
],
在html 中
<p *ngIf='checked' [innerHTML]="transformValue | html"></p>
本文介绍如何在Angular中创建自定义管道以安全地插入HTML内容。通过使用DomSanitizer的bypassSecurityTrustHtml方法,可以确保外部输入的HTML不会引起安全问题。文章详细展示了HtmlPipe的实现代码,并说明了如何在模块和HTML模板中使用。
3209

被折叠的 条评论
为什么被折叠?



