带有浏览器前缀的Angular 2 ngStyle

时间:2016-10-25 12:22:54

标签: angular angular2-security

这不起作用:

<div [style.-moz-column-count]="columns"></div>
<div [ngStyle]="{'-moz-column-count': columns}"></div>
<div style="-moz-column-count: {{columns}}"></div>

有没有办法避免Sanitizer?我找到了一个绕过该值的Pipe,但没有找到属性名称。

import {DomSanitizationService} from '@angular/platform-browser';
@Pipe({name: 'safe'})
export class SafePipe {
    constructor(sanitizer:DomSanitizationService){
        this.sanitizer = sanitizer;
    }

    transform(style) {
        return this.sanitizer.bypassSecurityTrustStyle(style);
    }
}

使用此属性的任何想法?

2 个答案:

答案 0 :(得分:2)

你需要这样写:

this.style = {
  "MozColumnCount": 3,
  "column-count": 3,
}

<div [ngStyle]="style">
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius.  
</div>

答案 1 :(得分:1)

我认为使用angular2 ngStyle可能无法实现。

你可以使用ngClass选项并像这样scss:

@for $i from 1 through 6 {
    .columns-#{$i} {
        -webkit-column-count: #{$i};
        -moz-column-count: #{$i};
        column-count: #{$i};
    }
}