Search code examples
angularangular2-template

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


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?


Solution

  • Using a ternary operator inside the ngStyle binding will function as an if/else condition.

    <div [ngStyle]="{'background-image': 'url(' + value ? image : otherImage + ')'}"></div>