最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 2, Adding calc() as inline style. Unsafe interpolation using parentheses - Stack Overflow

programmeradmin5浏览0评论

Angular 2 rc3

I am trying to dynamically add calc() to an element in a template. I have something like this.

template : `<div attr.style.width="{{width}}></div>"`

export myClass
{
    @Input() myInputObject:any;
    private width:string;


   ngOnInit() { this.setWidth()}

   private setWidth()
   {
       let percent = myInputObject.percent;
       this.width =  'calc(' + percent + '% - 20px)';
   }
}

If I use the parenthesis the ouput looks like this in the DOM.

<div style="unsafe"></div>

If I take out the parenthesis it works (sort of) It looks like this.

<div style="calc10% - 20px"></div>

This also doesn't work.

<div attr.style.width="calc({{width}} - 20px)">

Any help on how to add calc() to the template is much appreciated. Note I also tried replacing the parenthesis with &#40; and &#41;. That also came back as "unsafe".

Example: (rc1)

I am using rc3 in my environment. But I was able to reproduce the same issue with RC1 in plunker. I am assuming this is a security thing. But there must be a way to add calc() to a style attribute. Maybe there is a better way than this?

Angular 2 rc3

I am trying to dynamically add calc() to an element in a template. I have something like this.

template : `<div attr.style.width="{{width}}></div>"`

export myClass
{
    @Input() myInputObject:any;
    private width:string;


   ngOnInit() { this.setWidth()}

   private setWidth()
   {
       let percent = myInputObject.percent;
       this.width =  'calc(' + percent + '% - 20px)';
   }
}

If I use the parenthesis the ouput looks like this in the DOM.

<div style="unsafe"></div>

If I take out the parenthesis it works (sort of) It looks like this.

<div style="calc10% - 20px"></div>

This also doesn't work.

<div attr.style.width="calc({{width}} - 20px)">

Any help on how to add calc() to the template is much appreciated. Note I also tried replacing the parenthesis with &#40; and &#41;. That also came back as "unsafe".

Example: (rc1)

I am using rc3 in my environment. But I was able to reproduce the same issue with RC1 in plunker. I am assuming this is a security thing. But there must be a way to add calc() to a style attribute. Maybe there is a better way than this?

https://plnkr.co/edit/hmx5dL72teOyBWCOL0Jm?p=preview

Share Improve this question edited Jun 29, 2016 at 21:22 khollenbeck asked Jun 29, 2016 at 21:06 khollenbeckkhollenbeck 16.2k18 gold badges68 silver badges102 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 22

Calculated styles should be sanitized.

Here is the solution for you:

import {DomSanitizationService} from '@angular/platform-browser';

@Component({
  selector: 'my-app'
  template: `
    <div [style.width]="width">
      <h2>Hello {{name}}</h2>
    </div>
  `
})
export class App {

  private width:string;

  constructor(sanitizer: DomSanitizationService) {
    this.name = 'World'
    this.width = sanitizer.bypassSecurityTrustStyle("calc(10% - 20px)");
  }
}

You can also try using ngStyle instead:

[ngStyle]="{'width': 'calc(' + percent + '% - 20px)'}"

And just bind 'percent' value to the input value.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论