带有条件表达式的 AngularJS ng-style [英] AngularJS ng-style with a conditional expression

查看:57
本文介绍了带有条件表达式的 AngularJS ng-style的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这样处理我的问题的:

I am handling my issue like this:

ng-style="{ width: getTheValue() }"

但是为了避免在控制器端使用这个功能,我更愿意做这样的事情:

But to avoid having this function on the controller side, I would much prefer to do something like this:

ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"

我该怎么做?

推荐答案

正如@Yoshi 所说,从 angular 1.1.5 开始,您无需任何更改即可使用.

As @Yoshi said, from angular 1.1.5 you can use-it without any change.

如果你使用 angular <1.1.5,你可以使用ng-class.

If you use angular < 1.1.5, you can use ng-class.

.largeWidth {
    width: 100%;
}

.smallWidth {
    width: 0%;
}

// [...]

ng-class="{largeWidth: myVar == 'ok', smallWidth: myVar != 'ok'}"

这篇关于带有条件表达式的 AngularJS ng-style的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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