最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far