我是这样处理我的问题的:
ng-style="{ width: getTheValue() }"
但是为了避免在控制器端使用这个函数,我更倾向于这样做:
ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"
我该怎么做呢?
我是这样处理我的问题的:
ng-style="{ width: getTheValue() }"
但是为了避免在控制器端使用这个函数,我更倾向于这样做:
ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"
我该怎么做呢?
当前回答
一般来说,您可以结合使用ng-if和ng-style合并背景图像的条件更改。
<span ng-if="selectedItem==item.id"
ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_active.png)','background-size':'52px 57px','padding-top':'70px','background-repeat':'no-repeat','background-position': 'center'}"></span>
<span ng-if="selectedItem!=item.id"
ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_deactivated.png)','background-size':'52px 57px','padding-top':'70px','background-repeat':'no-repeat','background-position': 'center'}"></span>
其他回答
你可以这样做:
ng-style="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
编辑:
好吧,我以前不知道AngularJS通常指的是Angular v1版本,而只指Angular v2+
这个答案只适用于Angular
把这个留在这里,以备将来参考。
不知道它对你们来说是如何工作的,但在Angular 9中,我必须像这样用括号括起ngStyle:
[ng-style]="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
否则就不行
一般来说,您可以结合使用ng-if和ng-style合并背景图像的条件更改。
<span ng-if="selectedItem==item.id"
ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_active.png)','background-size':'52px 57px','padding-top':'70px','background-repeat':'no-repeat','background-position': 'center'}"></span>
<span ng-if="selectedItem!=item.id"
ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_deactivated.png)','background-size':'52px 57px','padding-top':'70px','background-repeat':'no-repeat','background-position': 'center'}"></span>
我正在使用ng-class添加风格:-
ng-class="column.label=='Description' ? 'tableStyle':
column.label == 'Markdown Type' ? 'Mtype' :
column.label == 'Coupon Number' ? 'couponNur' : ''
"
在angular.js中使用三元操作符和ng-class指令来给出样式。 然后在.css或.scss文件中定义类的样式。如:-
.Mtype{
width: 90px !important;
min-width: 90px !important;
max-width: 90px !important;
}
.tableStyle{
width: 129px !important;
min-width: 129px !important;
max-width: 129px !important;
}
.couponNur{
width: 250px !important;
min-width: 250px !important;
max-width: 250px !important;
}
就像@Yoshi说的,从angular 1.1.5开始你就可以不用做任何改变就可以使用它了。
如果你使用angular < 1.1.5,你可以使用ng-class。
.largeWidth {
width: 100%;
}
.smallWidth {
width: 0%;
}
// [...]
ng-class="{largeWidth: myVar == 'ok', smallWidth: myVar != 'ok'}"