ng-class

Description

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  av  a2s . c o  m-->
      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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Tutorial »




Introduction
Buildin Filters
Buildin Directives