AngularJS ng-style Directive The ng-style directive specifies the style attribute for the HTML element. The value of the ng-style attribute must be an object, or an expression returning an object. The object consists of CSS properties and values, in key value pairs.
simple example:
<div ng-style="isTrue && {'background-color':'green'} || {'background-color': 'blue'}" style="width:200px;height:100px;border:1px solid gray;"></div>
{'background-color':'green'} RETURN true
OR the same result:
<div ng-style="isTrue && {'background-color':'green'}" style="width:200px;height:100px;border:1px solid gray;background-color: blue"></div>
other conditional possibility:
<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>
As @Yoshi said, from angular 1.1.5 you can use-it without any change.
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'}"
You can achieve it like that:
ng-style="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
@jfredsilva obviously has the simplest answer for the question:
ng-style="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
However, you might really want to consider my answer for something more complex.
Ternary-like example:
<p ng-style="{width: {true:'100%',false:'0%'}[myObject.value == 'ok']}"></p>
Something more complex:
<p ng-style="{
color: {blueish: 'blue', greenish: 'green'}[ color ],
'font-size': {0: '12px', 1: '18px', 2: '26px'}[ zoom ]
}">Test</p>
If $scope.color == 'blueish'
, the color will be 'blue'.
If $scope.zoom == 2
, the font-size will be 26px.
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>
if you want use with expression, the rigth way is :
<span class="ng-style: yourCondition && {color:'red'};">Sample Text</span>
but the best way is using ng-class
On a generic note, you can use a combination of ng-if
and ng-style
incorporate conditional changes with change in background image.
<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>
For single css property
ng-style="1==1 && {'color':'red'}"
For multiple css properties below can be referred
ng-style="1==1 && {'color':'red','font-style': 'italic'}"
Replace 1==1 with your condition expression
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With