javascript - How to conditionally set specific class property with angular js? -


i have 1 div in want conditionally set background color property of class.i using boostrap progress bar , want use below class because contains custom settings progress bar.

below div:

.statistics .progress-bar {     background-color: black;     border-radius: 10px;     line-height: 20px;     text-align: center;     transition: width 0.6s ease 0s;     width: 0; }      <div class="statistics" ng-repeat="item in data">     <div class="progress-bar" role="progressbar" aria-valuenow="70"                                     aria-valuemin="0" aria-valuemax="100" ng-style="{'width' : ( item.statistics + '%' )}">      </div>   </div> 

condition below :

if statistics > 100     backgroundcolor=red; else      backgroundcolor=black; 

can me this??

you can using simple expression

ng-style="<condition> ? { <true-value> } : { <false-value> }" 

output

updated output

code:

<!doctype html>      <html ng-app="myapp">        <head>          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>      </head>        <body ng-controller="myctrl">            <div ng-style="item.statistics > 100 ? { 'background-color':'red', 'width': item.statistics + '%' }: { 'background-color':'yellow', 'width': item.statistics + '%'}">              <h2>$scope.statistics = {{statistics}}</h2>          </div>            <div ng-style="item.statistics2 > 100 ? { 'background-color':'red', 'width': item.statistics2 + '%' } : { 'background-color':'yellow', 'width': item.statistics2 + '%'}">              <h2>$scope.statistics2 = {{statistics2}}</h2>          </div>            <script>              var myapp = angular.module("myapp", []);                myapp.controller("myctrl", ['$scope', function($scope) {                  $scope.item = {};                  $scope.item.statistics = 30;                  $scope.item.statistics2 = 101;              }]);          </script>      </body>        </html>


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -