【问题标题】:Render CSS for innerHtml using angular2使用 angular2 为 innerHtml 渲染 CSS
【发布时间】:2017-02-18 22:25:00
【问题描述】:

我正在尝试使用 innerHTML 和从 SQL 获得的 html + css 字符串呈现 HTML 模板。

模板字符串示例:

<html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Template Name</title><style type="text/css"> p{ color:red; }</style> </head> <body> <h1>#headding#</h1> <p style="color:red;">#paragraph#</p><a href="#url#">#urltext#</a> </body> </html>

现在它可以很好地呈现 HTML,但看起来它会丢弃样式标签并只呈现其中的文本。

渲染示例:

HTML 渲染部分:

<div [innerHtml]="templateBody">
</div>

Home.component.ts 部分:

@Component({
    selector: "home",
    templateUrl: `client/modules/home/home.component.html`,
    encapsulation: ViewEncapsulation.Emulated
})
export class HomeComponent implements OnInit{
    templateBody: string;
.....other code
}

我已经尝试过封装:ViewEncapsulation.Emulated/None 等,尝试了内联 CSS,我尝试在 p 标签前附加 :host >>>。它们都呈现相同的效果。

有什么建议吗?

【问题讨论】:

    标签: angular innerhtml


    【解决方案1】:

    将其与 DomSanitizer with bypassSecurityTrustHtml 和 SafeHtml 一起使用,如下所示,

    演示: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: `
    
          <div  [innerHtml]="html | safeHtml"></div>
      `,
    })
    export class App {
      name:string;
      html: safeHtml;
      constructor() {
        this.name = 'Angular2'
        this.html = `<html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Template Name</title><style type="text/css"> p{ color:red; }</style> </head> <body> <h1>#headding#</h1> <p style="color:red;">#paragraph#</p><a href="#url#">#urltext#</a> </body> </html>`;
      }
    
    }
    

    【讨论】:

    • 欢迎@ShaunGroenewald
    【解决方案2】:

    注入Sanitizer 并将bypassSecurityTrustHtml(value: string) : SafeHtml 应用于HTML 内容,如https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html 所示,以使Angular2 知道您信任该内容。

    另见In RC.1 some styles can't be added using binding syntax

    【讨论】:

      【解决方案3】:

      我没有任何管道,只需将 DomSanitizer 和 SafeHtml 注入我的组件并在我的标记字符串上运行 bypassSecurityTrustHtml 即可。这让我可以防止我的内联样式被解析出来。

      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('<span style="color:##0077dd">this works</span>');
          }
      }
      

      在 foo.component.html 模板中

      <div [innerHtml]="html"></div>
      

      【讨论】:

      • 这篇文章由于缺少解释而被标记为低质量。尝试扩展您的答案。
      • @DerekBrown 解释已添加
      • @taylor michels 嗨,我已经使用了你的代码,它工作正常。但点击功能在该标签中不起作用。这是我的代码&lt;p *ngIf="value.item_df_id == 1" [innerHTML]="this.sanitizer.bypassSecurityTrustHtml(value.data_format_value)" (click)="showQuesBank(seq)"&gt;{{value.data_format_value}}&lt;/p&gt;
      猜你喜欢
      • 2015-10-18
      • 2017-08-21
      • 2017-02-10
      • 1970-01-01
      • 2017-03-24
      • 2017-08-05
      • 2017-02-09
      • 2016-05-15
      • 2017-11-10
      相关资源
      最近更新 更多