Search code examples
javascriptangularng-style

Convert number to degrees in ngStyle using Angular 8


{{result.percentage}} gives me a number from 0 to 100.

If I get 25, I want it to convert to -45 degrees or if I get 0, I want it to convert to -90 degrees or if I get 75, then it gets converted to 45 degrees.

[ngStyle]="{'transform': 'rotate(' + result.percentage + 'deg)'}"

Current, this gives an output style="transform: rotate(25deg);" which I want to convert to style="transform: rotate(-45deg);"

How do I go about this?

FYI, I need to bind the data of this speedometer: https://jsfiddle.net/bcgzrdfL/


Solution

  • Looks like you need a math whiz, not a framework expert, however I'm going to take a shot at an answer:

    [ngStyle]="{'transform': 'rotate(' + ((result.percentage * 1.8) - 90) + 'deg)'}"