---
title: "Adding multiple class using ng-class"  
description: "Adding multiple class using ng-class"  
author: "Revati S Misra"  
published: 2024-06-17  
updated: 2024-06-18  
canonical: https://www.mindstick.com/forum/160751/adding-multiple-class-using-ng-class  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs ng-class"]  
reading_time: 3 minutes  

---

# Adding multiple class using ng-class

[Adding multiple](https://www.mindstick.com/forum/33435/adding-multiple-views-to-view-controller-inside-a-tab-controller) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) using ng-class

## Replies

### Reply by Ashutosh Patel

### Add multiple classes using `ng-class`

In AngularJS, you can use the `ng-class` directive to dynamically apply [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) classes to an element based on expressions in your controller or directly in your template.

Here’s how you can do this.

#### Use object syntax

You can use object syntax with ng-class where each key is a class name and its value is a boolean expression indicating whether the class should be used.

```html
<div ng-class="{ 'class1': condition1, 'class2': condition2, 'class3': condition3 }">
 <!-- Content here -->
</div>
```

## in the above syntax

class1, class2, and class3 are optional class names.\
condition1, condition2, and condition3 are boolean expressions that specify the scope of your controller indicating whether each class should be used (true) or not (false).

## Example

## HTML

```html
<div ng-class="{ 'red': isError, 'bold': isImportant, 'italic': isItalic }">
 <!-- Content here -->
</div>
```

## Controller

```plaintext
$scope.isError = true;
$scope.isImportant = false;
$scope.isItalic = true;
```

In this case `<div>` have **red** and **italic** classes, because `isError`and `isItalic`are **true**, `isImportant`is **false**.

#### Array Syntax

You can also use an array syntax with `ng-class` where each element in the array is a class name added with no entry.

```html
<div ng-class="[ 'class1', 'class2', 'class3' ]">
 <!-- Content here -->
</div>
```

## Example-

```html
<div ng-class="[ 'alert', 'alert-' + type, { 'alert-dismissible': dismissible } ]">
 <!-- Content here -->
</div>
```

## In the example above

'`alert`' and '`alert-`' + **type** are added with nothing as class.\
`{ 'alert-dismissible': dismissible }` There is an object to add the **alert-dismissible** class if `dismissible`evaluates to `true`.

#### Combining Methods

You can also combine both methods for more flexibility.

```html
<div ng-class="[ 'class1', { 'class2': condition2 }, 'class3' ]">
 <!-- Content here -->
</div>
```

## In the example above

`class1` is added unconditionally.\
`class2` is added if **condition2** evaluates to true.\
`class3` is added unconditionally.

## Example-

here is a simple example to add/remove multiple classes using `ng-class`

```html
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Apply Filter Example</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <!-- bootstrap cdn link -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- angular js cdn link -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>

    <style>
        /* styling the text based on classes */
        .highlight {
          background-color: yellow;
        }
        .italic {
          font-style: italic;
        }
        .underline {
          text-decoration: underline;
        }
      </style>
</head>

<body>
    <div data-ng-controller="MyController" class="container my-4">
        <!-- text in which classes add/remove dynamically -->
        <h2 data-ng-class="{ 'highlight': isHighlighted, 'italic': isItalic, 'underline': isUnderlined }">
            Example Text
        </h2>
        <!-- multiple classes add dynamically based on checked -->
        <label><input type="checkbox" ng-model="isHighlighted" checked= "checked"> Highlight</label><br>
        <label><input type="checkbox" ng-model="isItalic"> Italic</label><br>
        <label><input type="checkbox" ng-model="isUnderlined"> Underline</label>
    </div>

    <script>
        angular.module("myApp", [])
        .controller("MyController", function($scope){
            // by default all the classes is false
            $scope.isHighlighted = false;
            $scope.isItalic = false;
            $scope.isUnderlined = false;
        });

    </script>

</body>
</html>
```

**Output-** \
![Adding multiple class using ng-class](https://www.mindstick.com/mindstickforums/d1f2632f-af27-46a2-977f-6ec8cd01d63f/images/d649b98c-a893-4f3c-8d01-cee1e64d12e8.jpg)\

**Also, Read:** [How to use a filter in a controller?](https://www.mindstick.com/forum/160750/how-to-use-a-filter-in-a-controller)


---

Original Source: https://www.mindstick.com/forum/160751/adding-multiple-class-using-ng-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
