Combine [NgStyle] With Condition (if..else)

user1294914 picture user1294914 · May 5, 2016 · Viewed 107.2k times · Source

I have read NgStyle Documentation For Angular 2, and it is a little bit confusing for me. How do I use NgStyle with condition like (if...else) to set background image of any element?

Answer

Zohar picture Zohar · Sep 7, 2017

One can also use this kind of condition:

<div [ngStyle]="myBooleanVar && {'color': 'red'}"></div>

It requires a bit less string concatenation...