AngularJS Tutorial - ng-class








ng-class dynamically sets the class of an element by binding an expression.

The following code shows how to ng-class.

Example


<!doctype html>
<html ng-app="myApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script>
  <style>
    .red {<!--  ww  w  .  j ava  2s  . com-->
      background-color: red;
    }
  </style>
</head>
<body>

<div ng-controller="MyController">
  <div ng-class="{red: x > 0}">
    Label
  </div>
  <button ng-click="x = 1" ng-init="x = -1">click</button>
</div>

  <script>
    angular.module('myApp', [])
    .controller('MyController', function($scope) {
    })
  </script>

</body>
</html>

The code above is rendered as follows: