angular 2+ innerHTML属性中内联样式丢失
通过属性绑定的innerHTML,把字符串里面的html解析
解析是没问题的,但一些内联样式会丢失掉
为了不丢掉样式,需要自定义一个管道来解决这个问题
html.pipe.ts
import {Pipe, PipeTransform} from "@angular/core"; import {DomSanitizer} from "@angular/platform-browser"; @Pipe({ name: "html" }) export class HtmlPipe implements PipeTransform{ constructor (private sanitizer: DomSanitizer) { } transform(style) { return this.sanitizer.bypassSecurityTrustHtml(style); } }
html使用
<p [innerHTML]="content | html"></p>