使用插值表达式{{}}直接显示html内容,则html的标签不会被解析
<div class="content" [innerHTML]="article.content"></div>
问题
新建一个用来过滤数据的管道,意思就是告诉angular,过我这个管道的数据就是安全的
ng g p ./pipe/bypassSecurityTrustHtml --spec=false
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'bypassSecurityTrustHtml'
})
export class BypassSecurityTrustHtmlPipe implements PipeTransform {
constructor(private domSanitizer: DomSanitizer) {}
transform(html: any, args?: any): any {
return this.domSanitizer.bypassSecurityTrustHtml(html);
}
}
<div class="content" [innerHTML]="article.content | bypassSecurityTrustHtml"></div>