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

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

问题描述

我已经阅读了 Angular 2 的 NgStyle 文档,这对我来说有点困惑.如何使用 NgStyle 和 (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?

推荐答案

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天全站免登陆