我这样处理我的问题:
ng-style="{ width: getTheValue() }"
但为了避免在控制器端具有此功能,我更愿意做这样的事情:
ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"
我怎样才能做到这一点?
正如@Yoshi所说,从角度1.1.5你可以使用它 - 没有任何改变 .
如果使用angular <1.1.5,则可以使用ng-class .
.largeWidth { width: 100%; } .smallWidth { width: 0%; } // [...] ng-class="{largeWidth: myVar == 'ok', smallWidth: myVar != 'ok'}"
简单的例子:
<div ng-style="isTrue && {'background-color':'green'} || {'background-color': 'blue'}" style="width:200px;height:100px;border:1px solid gray;"></div>
{'background-color':'green'}返回true
或者相同的结果:
<div ng-style="isTrue && {'background-color':'green'}" style="width:200px;height:100px;border:1px solid gray;background-color: blue"></div>
其他条件可能性:
<div ng-style="count === 0 && {'background-color':'green'} || count === 1 && {'background-color':'yellow'}" style="width:200px;height:100px;border:1px solid gray;background-color: blue"></div>
你可以这样做:
ng-style="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
@jfredsilva显然有the simplest answer的问题:
ng-style =“{'width':( myObject.value =='ok')?'100%':'0%'}”
However, you might really want to consider my answer for something more complex.
类似三元的例子:
<p ng-style="{width: {true:'100%',false:'0%'}[myObject.value == 'ok']}"></p>
更复杂的东西:
<p ng-style="{ color: {blueish: 'blue', greenish: 'green'}[ color ], 'font-size': {0: '12px', 1: '18px', 2: '26px'}[ zoom ] }">Test</p>
如果 $scope.color == 'blueish' ,颜色将为'blue' .
$scope.color == 'blueish'
如果 $scope.zoom == 2 ,则字体大小为26px .
$scope.zoom == 2
angular.module('app',[]); function MyCtrl($scope) { $scope.color = 'blueish'; $scope.zoom = 2; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script> <div ng-app="app" ng-controller="MyCtrl" ng-style="{ color: {blueish: 'blue', greenish: 'green'}[ color ], 'font-size': {0: '12px', 1: '18px', 2: '26px'}[ zoom ] }"> color = {{color}}<br> zoom = {{zoom}} </div>
如果你想使用表达式,那么严格的方法是:
<span class="ng-style: yourCondition && {color:'red'};">Sample Text</span>
但最好的方法是使用ng-class
在通用注释中,您可以使用 ng-if 和 ng-style 的组合将条件更改与背景图像中的更改相结合 .
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="<$scope.var><condition> ? { '<css-prop-1>':((<value>) / (<value2>)*100)+'%', '<css-prop-2>':'<string>' } : { '<css-prop-1>':'<string>', '<css-prop-2>':'<string>' }"
其中 <value> 是$ scope属性值 . 例如:
<value>
ng-style="column.histograms.value=>0 ? { 'width':((column.histograms.value) / (column.histograms.limit)*100)+'%', 'background':'#F03040' } : { 'width':'1px', 'background':'#2E92FA' }"
这允许一些计算器进入css属性值 .
我在下面做了多个独立的条件,它就像魅力一样:
<div ng-style="{{valueFromJS}} === 'Hello' ? {'color': 'red'} : {'color': ''} && valueFromNG-Repeat === '{{dayOfToday}}' ? {'font-weight': 'bold'} : {'font-weight': 'normal'}"></div>
对于单个css属性
ng-style="1==1 && {'color':'red'}"
对于下面的多个css属性,可以参考
ng-style="1==1 && {'color':'red','font-style': 'italic'}"
用条件表达式替换1 == 1
我使用ng-class添加样式: -
ng-class="column.label=='Description' ? 'tableStyle': column.label == 'Markdown Type' ? 'Mtype' : column.label == 'Coupon Number' ? 'couponNur' : '' "
使用 ternary operator 和 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; }
10 回答
正如@Yoshi所说,从角度1.1.5你可以使用它 - 没有任何改变 .
如果使用angular <1.1.5,则可以使用ng-class .
简单的例子:
{'background-color':'green'}返回true
或者相同的结果:
其他条件可能性:
你可以这样做:
@jfredsilva显然有the simplest answer的问题:
However, you might really want to consider my answer for something more complex.
类似三元的例子:
更复杂的东西:
如果
$scope.color == 'blueish'
,颜色将为'blue' .如果
$scope.zoom == 2
,则字体大小为26px .如果你想使用表达式,那么严格的方法是:
但最好的方法是使用ng-class
在通用注释中,您可以使用
ng-if
和ng-style
的组合将条件更改与背景图像中的更改相结合 .这个三元运算符的语法也可以工作:
其中
<value>
是$ scope属性值 . 例如:我在下面做了多个独立的条件,它就像魅力一样:
对于单个css属性
对于下面的多个css属性,可以参考
用条件表达式替换1 == 1
我使用ng-class添加样式: -
使用 ternary operator 和 angular.js 中的ng-class指令来给出样式 . 然后在 .css 或 .scss 文件中定义类的样式 . 例如: -