1

I need to add a margin to an element with some calculations as below:

[ngStyle]="{'margin-left': calc(sustainScrore && sustainScrore.scoring && sustainScrore.scoring.score ? sustainScrore.scoring.score + '%' : '0%')}"

This seems to throw an error :

ERROR TypeError: _co.calc is not a function Looking for a solution.

1 Answer 1

1
[ngStyle]="{ 'margin-left': 'calc(' + (sustainScrore?.scoring?.score || '0') + '%)' }"

This should do the trick.

Your issue was not considering calc as a string : Angular was looking for a funtion to call, while you wanted to create a CSS property. I also reduced your code with the safe navigation operator, leaving you with cleaner code.

if you want to reduce it further, use the style.XXX notation :

[style.margin-left]="'calc(' + (sustainScrore?.scoring?.score || '0') + '%)'"
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, if you think it derserves it, consider marking my answer as the solution in order to help people having the same issue resolve it !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.