相关疑难解决方法(0)

将[NgStyle]与条件组合(if..else)

我已经阅读了Angular2的NgStyle文档.对我来说有点困惑.关于如何使用NgStyle与条件(if ... else)来设置任何元素的背景图像的任何想法?

angular2-template angular

42
推荐指数
5
解决办法
6万
查看次数

使用 Angular 2+ [innerHTML] 添加包含样式属性的 html

我正在使用 Angular 2+ [innerHTML] 输入来插入 HTML 格式,包括样式标签。

在我的模板中,我有类似的东西:

<span [innerHTML]="someVar"></span>
Run Code Online (Sandbox Code Playgroud)

在我的组件中,我有:

someVar = `<span style="background-color:#990000">test</span>`;
Run Code Online (Sandbox Code Playgroud)

我收到警告:

WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).
Run Code Online (Sandbox Code Playgroud)

在输出中,插入的跨度完好无损,减去样式属性。

所以我使用了这篇文章中的管道:

/sf/ask/2595380721/

看起来像:

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

@Pipe({name: 'safeHtml'})
export class SanitizeHtml implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) {}

  transform(html): SafeHtml {
    // return this.sanitizer.bypassSecurityTrustStyle(style);
    return this.sanitizer.bypassSecurityTrustHtml(html);
    // return this.sanitizer.bypassSecurityTrustScript(value);
    // return this.sanitizer.bypassSecurityTrustUrl(value);
    // return this.sanitizer.bypassSecurityTrustResourceUrl(value);
  }
}
Run Code Online (Sandbox Code Playgroud)

这与以前没有区别,尽管我不确定我是否以正确的方式使用它......

如何使用innerHTML让Angular保留我的样式属性?

javascript css angular

3
推荐指数
1
解决办法
6284
查看次数

标签 统计

angular ×2

angular2-template ×1

css ×1

javascript ×1