Is there anyway you can do calculation in the style binding target in Angular4 ?
I've tried
[style.width]="{{1+1}}"
[style.width]="{{1+1}}px"
[style.width]="{{1px+1px}}"
You can use [style.width.px]="1 + 1"
in order to achieve that.
this should work
[style.width]="1+1+'px'"
Basically, when you're using []
, the must be expression and you should never put {{}}
in the value, and this is a general Angular2 rule when you're using attribute binding.
[style.width]="{{1+1}}" is wrong because of `{{}}` in the value
[style.height.px]="200"
[style.height.px]="200 + 50"
[style.height.px]="_commonService.screenHeight"
[style.height.px]="_commonService.screenHeight + 50"
[style.height.px]="_commonService.screenHeight - (isHomePage == true?80:140)"
/* Other Examples **/
[ngStyle]="{'margin-top': isHomePage == true ? '0px' : '60px' }"
You should avoid doing style calculation inside your view. Simply create a function to handle this calculation and only return the result in view.