Open In App

How to toggle class using AngularJS ?

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: 



Example: This example describes the toggling of the class in AngularJS by specifying the value in the terms of 0 & 1.




<!DOCTYPE HTML>
<html>
 
<head>
    <script src=
    </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: 

Example: This example describes toggling the class in AngularJS by specifying the boolean value.




<!DOCTYPE HTML>
<html>
 
<head>
    <script src=
    </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:

 


Article Tags :