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
One can also use this kind of condition:
<div [ngStyle]="myBooleanVar && {'color': 'red'}"></div>
It requires a bit less string concatenation...
To add and simplify Günter Zöchbauer's the example incase using (if...else) to set something else than background image :
<p [ngStyle]="value == 10 && { 'font-weight': 'bold' }">
I have used the below code in my existing project
<div class="form-group" [ngStyle]="{'border':isSubmitted && form_data.controls.first_name.errors?'1px solid red':'' }">
</div>
Using a ternary operator inside the ngStyle
binding will function as an if/else condition.
<div [ngStyle]="{'background-image': 'url(' + value ? image : otherImage + ')'}"></div>