Angular2 innerHtml绑定删除样式属性

Sam*_*rof 55 styles angular

我的问题,当我使用innererHtml绑定时 - angular2删除所有样式属性.这对我来说很重要,因为在我的任务中 - html是在服务器端生成的,具有所有样式.例:

@Component({
  selector: 'my-app',
  template: `
    <input type="text" [(ngModel)]="html">
    <div [innerHtml]="html">
    </div>
  `,
})
export class App {
  name:string;
  html: string;
  constructor() {
    this.name = 'Angular2'
    this.html = "<span style=\"color:red;\">1234</span>";
  }
}
Run Code Online (Sandbox Code Playgroud)

但是在DOM中我只看到1234而且这个文本不是红色的.

http://plnkr.co/edit/UQJOFMKl9OwMRIJ38U8D?p=preview

谢谢!

yur*_*zui 124

你可以利用DomSanitized它来避免它.

最简单的方法是创建自定义管道,如:

import { DomSanitizer } from '@angular/platform-browser'
import { PipeTransform, Pipe } from "@angular/core";

@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform  {
  constructor(private sanitized: DomSanitizer) {}
  transform(value) {
    return this.sanitized.bypassSecurityTrustHtml(value);
  }
}
Run Code Online (Sandbox Code Playgroud)

所以你可以使用它:

<div [innerHtml]="html | safeHtml"></div>
Run Code Online (Sandbox Code Playgroud)

Plunker示例

  • 管道名称应该是“unsafeHtml”,因为它实际上会让不安全的 html 进入 dom。这个管道里没有什么“安全”的东西 (5认同)
  • 很棒的解决方案!我唯一改变的是我将 `pure: true` 添加到了 `@Pipe` 装饰器中,这样 Angular 就知道不会在每次更改检测时一遍又一遍地重新计算它,并且只有在 `html` (输入)已更改。至少我认为应该发生这样的事情:) (2认同)
  • 这可能会让您面临 XSS 攻击。 (2认同)
  • 绕过消毒剂以信任任何内容可能是安全问题。由于Angular不是专门的消毒库,因此对可疑内容过分热衷以不承担任何风险。例如,它删除了几乎所有属性。您可以将清理委托给专用的库-DOMPurify。这是我制作的包装器库,可轻松将Angular与DOMPurify结合使用:https://github.com/TinkoffCreditSystems/ng-dompurify (2认同)
  • 这会产生安全问题。在实施此解决方案之前,请阅读@a darren 的回答! (2认同)

mve*_*and 33

我通过完成所需的导入改进了yurzui的例子:

import {DomSanitizer} from '@angular/platform-browser';
import {PipeTransform, Pipe} from "@angular/core";

@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform  {
  constructor(private sanitized: DomSanitizer) {}
  transform(value) {
    return this.sanitized.bypassSecurityTrustHtml(value);
  }
}
Run Code Online (Sandbox Code Playgroud)

我还必须在app.module.ts文件中添加该类

import ...
import {SafeHtmlPipe} from "./pipes/safehtml.pipe";
@NgModule({
    declarations: [
        AppComponent,
        ...,
        SafeHtmlPipe  <--
    ],
    imports: [...],
    providers: [...],
    bootstrap: [AppComponent]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)


a d*_*ren 5

请注意,sanitizer有几种方法可以信任内容,例如

return this.sanitizer.bypassSecurityTrustStyle(value);
return this.sanitizer.bypassSecurityTrustHtml(value);
return this.sanitizer.bypassSecurityTrustXxx(value); // - see docs [1]
Run Code Online (Sandbox Code Playgroud)

通过/sf/answers/2876236541/

因此,这bypassSecurityTrustStyle可能也是您想要的,因为这将在HTML内容(value)中显示内联样式。

[1]文档:https : //angular.io/docs/ts/latest/api/platform-b​​rowser/index/DomSanitizer-class.html

  • 这不应该是公认的答案,因为它是错误的。`bypassSecurityTrustStyle` 返回 `SafeStyle`,而 `[innerHTML]` 需要 `SafeHtml`。如果你使用它,它会抛出:“需要一个安全的 HTML,有一个样式”。因此,如果您的 html 内容具有内联样式,则需要外部清理程序和“bypassSecurityTrustHtml()”的组合,如下面的评论中所述(/sf/ask/2773960521/ /39630507#comment104193367_39630507) 应该是要走的路。 (7认同)
  • 在我看来,这应该是公认的答案。仅使用`bypassSecurityTrustHtml()` 就可以运行恶意javascript。如果您只需要绕过样式,那么使用`bypassSecurityTrustStyle()` 绕过会更好。 (2认同)

归档时间:

查看次数:

40892 次

最近记录:

7 年,8 月 前