How to toggle class using AngularJS ? Last Updated : 04 Aug, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will toggle the class of an element with the help of AngularJS. The toggling of the class can be done in 2 ways, i.e., either specifying the value to 0 or 1, depending upon the condition satisfied, by initially defining the value as 0, or depending on the boolean value, i.e., true or false, with satisfying the required condition, by initially setting the value as true. Approach 1: In this example, when a button is clicked then the class of an element is changed. So, a function is called when the button is clicked. That function toggles the class from val to !val(means 0 to 1 and vice-versa). In the function called, we simply check if it is class1 then change it to class2 else do the opposite.Example: This example describes the toggling of the class in AngularJS by specifying the value in the terms of 0 & 1. HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function($scope) { $scope.val = 0; $scope.toggleClass = function(sel) { if($scope.val == 0) { $scope.val = 1; } else { $scope.val = 0; } }; }); </script> <style> .class1 { color: white; background: blue; } .class2 { color: white; background: green; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Toggle Class in angularJS </h3> <div ng-app="app"> <div ng-controller="controller"> <div ng-class="{'class1':!val, 'class2': val}"> {{ val }} </div> <br> <a href="javascript:void(0);" ng-click='toggleClass();'> Click to toggle class </a> </div> </div> </body> </html> Output: Approach 2: This example is somewhat similar to the previous one but used boolean values in place of 0 and 1. So, a function is called when the button is clicked. That function toggles the class from val to !val(means true to false and vice-versa). In the function called, we simply check if it is class1 then change it to class2 else do the opposite.Example: This example describes toggling the class in AngularJS by specifying the boolean value. HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function($scope) { $scope.val = true; $scope.toggleClass = function(sel) { if($scope.val == true) { $scope.val = false; } else { $scope.val = true; } }; }); </script> <style> .class1 { color: white; background: blue; } .class2 { color: white; background: green; } #div { height: 50px; width: 130px; color: white; margin: 0 auto; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Toggle Class in angularJS </h3> <div ng-app="app"> <div ng-controller="controller"> <div id='div' ng-class="{'class1':!val, 'class2': val}"> {{ val }} </div> <br> <a href="javascript:void(0);" ng-click='toggleClass();'> Click to toggle class </a> </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to toggle class using AngularJS ? P PranchalKatiyar Follow Improve Article Tags : Web Technologies HTML AngularJS HTML-Misc AngularJS-Misc +1 More Similar Reads How to Toggle a Class in Vue.js ? In this article, we will use Vue.js component with a simple HTML template containing <div> and button elements. The goal is to toggle the presence of a CSS class on the <div> element when the button is clicked. By understanding the concepts of data binding, conditional rendering, and eve 3 min read How to use *ngIf else in AngularJS ? Introduction: The ngIf directive is used to show or hide parts of an angular application. It can be added to any tags, it is a normal HTML tag, template, or selectors. It is a structural directive meaning that it includes templates based on a condition constrained to boolean. When the expression eva 3 min read How to disable buttons using AngularJS ? In this article, we will see how to disable the button using the particular directive in AngularJS. Sometimes, we need to disable the button, and link on the click event. This task can be accomplished by implementing the ng-disabled directive that is used to enable or disable HTML elements. Syntax: 2 min read AngularJS ng-class Directive The ng-class Directive in AngularJS is used to specify the CSS classes on HTML elements. It is used to dynamically bind classes on an HTML element. The value for the ng-class has either string, an object, or an array. It must contain more than one class name, which is separated by space, in the case 2 min read AngularJS ng-class-even Directive The ng-class-even Directive in AngularJS is used to specify the CSS classes on every even appearance of HTML elements. It is used to dynamically bind classes on every even HTML element. If the expression inside the ng-class-even directive returns true then only the class is added else it is not adde 2 min read AngularJS ng-class if-else Expression In the AngularJS framework, Conditional statements, like if-else expressions, are a feature that helps many web developers like us to apply different conditions in AngularJS applications. This can be used with the ng-class directive that allows us to control the styling of the elements based on the 5 min read How to make Tooltip using Angular UI Bootstrap ? In this article we will see how to make Dropdown using Angular UI bootstrap. Angular UI Bootstrap is an Angular JS framework created by Angular UI developers for providing better UI which can be used easily. Syntax: <div uib-tooltip></div> Download AngularUI from the link: https://angula 2 min read How To Style Components Using Angular ngClass? Styling components effectively is important in web development, and Angular offers various ways to apply CSS to your components. One of the most powerful and flexible methods is using the ngClass directive. The ngClass directive allows you to dynamically add or remove CSS classes based on component 5 min read Angular Conditional Class with *ngClass In this article, we will see the basic implementation of the Conditional class with *ngClass in Angular, along with understanding their implementation with the help of examples. Conditional class with *ngClassAngular's *ngClass directive allows us to apply CSS classes conditionally to an element. It 5 min read How to make Pagination using Angular UI Bootstrap ? In this article, we will see how to make Dropdown using Angular UI bootstrap Angular UI Bootstrap is an Angular JS framework created by Angular UI developers for providing better UI which can be used easily. Syntax: <div uib-pagination></div> Download AngularUI from the link: https://ang 1 min read Like