2

How do I use the Angular style binding syntax with the css calc() function for dynamically setting the width of an element.

With React I could simply write :-

<div style = {{
   width: `calc((${currentStep - 1 } * (100% / ${steps - 1})) - 6px)`
}}>

</div>

With Angular I want to accomplish the same thing :-

<div
[style.width] = `calc((${currentStep - 1 } * (100% / ${steps - 1})) - 6px)`
>
  
</div>
Lakshya Thakur
  • 8,030
  • 1
  • 12
  • 39
I_sam
  • 53
  • 6

1 Answers1

0

You can try something like:

[style. width]="getWidth(currentStep, steps)"

Lakshya Thakur
  • 8,030
  • 1
  • 12
  • 39
Yugi Oh
  • 43
  • 1
  • 7
  • Thanks Yugi, That doesn't work either, how ever this approach works with ngStyle. But the problem is I want to use the [style.width[] approach without having a method in my class file – I_sam Feb 12 '21 at 11:17
  • 1
    That is impossible in angular, you can't use [..]={{..}} See: https://stackoverflow.com/questions/40203279/parser-error-got-interpolation-where-expression-was-expected – Yugi Oh Feb 12 '21 at 11:39
  • You can do `style.width="stuff with {{}}"`, though; it's just the [bracket\] syntax you can't use. But since you can't use the `calc` built-in, it doesn't really help matters. – Roddy of the Frozen Peas Feb 12 '21 at 13:20