Render CSS for innerHtml using angular2

前端 未结 3 1008
走了就别回头了
走了就别回头了 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:05

    Use it with DomSanitizer with bypassSecurityTrustHtml and SafeHtml as shown below,

    DEMO : https://plnkr.co/edit/eBlzrIyAl0Il1snu6WJB?p=preview

    import { DomSanitizer } from '@angular/platform-browser'
    
    @Pipe({ name: 'safeHtml'})
    export class SafeHtmlPipe implements PipeTransform  {
      constructor(private sanitized: DomSanitizer) {}
      transform(value) {
        console.log(this.sanitized.bypassSecurityTrustHtml(value))
        return this.sanitized.bypassSecurityTrustHtml(value);
      }
    }
    
    @Component({
      selector: 'my-app',
      template: `
    
          
    `, }) export class App { name:string; html: safeHtml; constructor() { this.name = 'Angular2' this.html = ` Template Name

    #headding#

    #paragraph#

    #urltext# `; } }

提交回复
热议问题