Render CSS for innerHtml using angular2

前端 未结 3 1007
走了就别回头了
走了就别回头了 2021-01-12 14:39

I am trying to render a HTML template using innerHTML and a html + css string I get from SQL.

Template string example:



        
3条回答
  •  抹茶落季
    2021-01-12 15:08

    I did it without any pipes and just by injecting DomSanitizer and SafeHtml into my component and running bypassSecurityTrustHtml on my markup string. This allowed me to keep my inline styles from being parsed out.

    import { Component, OnInit } from '@angular/core';
    import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
    
    @Component({
        selector: "foo",
        templateUrl: "./foo.component.html"
    })
    
    export class FooComponent { 
        html: SafeHtml;
        constructor(private sanitizer: DomSanitizer) {
            this.html = this.sanitizer.bypassSecurityTrustHtml('this works');
        }
    }
    

    and in foo.component.html template

提交回复
热议问题