将[NgStyle]与条件结合使用(if..else) [英] Combine [NgStyle] With Condition (if..else)

查看:212
本文介绍了将[NgStyle]与条件结合使用(if..else)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读有关Angular 2的NgStyle文档,这对我来说有些困惑.如何在条件类似(if ... else)的情况下使用NgStyle来设置任何元素的背景图片?

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?

推荐答案

ngStyle绑定内使用三元运算符将作为if/else条件.

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

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

这篇关于将[NgStyle]与条件结合使用(if..else)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆