Angular : sanitizing HTML stripped some content on css style

前端 未结 1 992
情书的邮戳
情书的邮戳 2020-12-30 11:23

I am using a wysiwyg editor in my Angular component, when i try to preview the content of the editor, (after i apply center to the text), i get this warning:

相关标签:
1条回答
  • 2020-12-30 11:42

    This is by design in Angular 2+ for security reasons. You can workaround it by using the DomSanitizer class.

    For example you can make a pipe that prevents sanitization of the value:

    import { Pipe, PipeTransform } from '@angular/core';
    import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
    
    @Pipe({ name: 'noSanitize' })
    export class NoSanitizePipe implements PipeTransform {
       constructor(private domSanitizer: DomSanitizer) {
    
       }
       transform(html: string): SafeHtml {
          return this.domSanitizer.bypassSecurityTrustHtml(html);
       }
    }
    

    Then you can use it in binding for example like this:

    <div [innerHTML]="htmlText | noSanitize">
    </div>
    
    0 讨论(0)
提交回复
热议问题