AngularJS Tutorial - Get class in array








The following code shows how to use ng click event to change style.

Example


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<style type='text/css'>
.red {<!-- ww  w  .  j a va  2s . c o m-->
    background-color: red;
}

.green {
    background-color: green;
}

.blue {
    background-color: blue;
}
</style>
  
</head>
<body>
  <div ng-app="myApp" ng-controller="MainCtrl">
    <div ng-init="index=0; classes=['red', 'green', 'blue']" 
         ng-click="index = index + 1" 
         ng-class="classes[index % classes.length]">Test 1 2 3.</div>
</div>
<script type='text/javascript'>
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {});
</script>
</body>
</html>

The code above is rendered as follows: