Use model value to control style class

Description

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   ww w  .  j  av a  2s . com-->
    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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities