Toggle ng-class with model value

Description

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 w w  w  .  java 2  s. 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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities