AngularJS Tutorial - Toggle ng-class with model value








The following code shows how to Toggle ng-class with model value.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<style type='text/css'>
.selected { border-top: 1px solid red }
.other { border-right: 2px dashed green; }
</style><!--from  www.j  a v a  2s.  co m-->
<script type='text/javascript'>

function Ctrl($scope) {
    $scope.class2 = 'selected';
    $scope.toggle = true;
}
</script>
</head>
<body ng-app>
  <div ng-controller="Ctrl">
    <div ng-click="class1='selected'; class2=''" ng-class="class1">click me</div>
    <div ng-class="class2" class="other">hi</div>
    <button ng-class="{selected: toggle}" ng-click="toggle=!toggle">toggle button</button>
</div>
</body>
</html>

The code above is rendered as follows: