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? 回答1: Using a ternary operator inside the ngStyle binding will function as an if/else condition. <div [ngStyle]="{'background-image': 'url(' + value ? image : otherImage + ')'}"></div> 回答2: One can also use this kind of condition: <div [ngStyle]="myBooleanVar && {'color': 'red'}"></div> It requires a bit less