我是这样处理我的问题的:
ng-style="{ width: getTheValue() }"
但是为了避免在控制器端使用这个函数,我更倾向于这样做:
ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"
我该怎么做呢?
我是这样处理我的问题的:
ng-style="{ width: getTheValue() }"
但是为了避免在控制器端使用这个函数,我更倾向于这样做:
ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"
我该怎么做呢?
当前回答
对于单个css属性
ng-style="1==1 && {'color':'red'}"
对于下面的多个css属性可以参考
ng-style="1==1 && {'color':'red','font-style': 'italic'}"
用条件表达式替换1==1
其他回答
你可以这样做:
ng-style="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
就像@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'}"
一般来说,您可以结合使用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;
}
对于单个css属性
ng-style="1==1 && {'color':'red'}"
对于下面的多个css属性可以参考
ng-style="1==1 && {'color':'red','font-style': 'italic'}"
用条件表达式替换1==1