AngularJS Tutorial - Use model value to control style class








The following code shows how to use model value to control style class.

Example


<!doctype html>
<html ng-app>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<style type='text/css'>
.test1 {<!--from w  ww  . ja v a  2s.  c om-->
    color: blue;
}
</style>
</head>
<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <div ng-class="{test1: selected}">
        Text1
    </div>
    <div class="test1">
        Text2
    </div>
    <button ng-click="click()">Click</button>
</div>
<script type='text/javascript'>
var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.selected = true;
    $scope.click = function () {
      $scope.selected = false;
    };
}
</script>
</body>
</html>

The code above is rendered as follows: