Improve this Doc View Source

## Overview

The `ngClass` directive allows you to dynamically set CSS classes on an HTML element by databinding an expression that represents all classes to be added.

The directive operates in three different ways, depending on which of three types the expression evaluates to:

1. If the expression evaluates to a string, the string should be one or more space-delimited class names.

2. If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.

3. If the expression evaluates to an array, each element of the array should either be a string as in type 1 or an object as in type 2. This means that you can mix strings and objects together in an array to give you more control over what CSS classes appear. See the code below for an example of this.

The directive won't add duplicate classes if a particular class was already set.

When the expression changes, the previously added classes are removed and only then are the new classes added.

## Known Issues

You should not use interpolation in the value of the `class` attribute, when using the `ngClass` directive on the same element. See here for more info.

## Directive Info

- This directive executes at priority level 0.

## Usage

- as attribute:

      <ANY
        ng-class="expression">
      ...
      </ANY>

- as CSS class:

      <ANY class="ng-class: expression;"> ... </ANY>

### Arguments

Param | Type | Details
--- | --- | ---
ngClass | expression | Expression to eval. The result of the evaluation can be a string representing space delimited class names, an array, or a map of class names to boolean values. In the case of a map, the names of the properties whose values are truthy will be added as css classes to the element.

## Animations

Animation | Occurs
--- | ---
addClass | just before the class is applied to the element
removeClass | just before the class is removed from the element
setClass | just before classes are added and classes are removed from the element at the same time

### ngClass and pre-existing CSS3 Transitions/Animations

The ngClass directive still supports CSS3 Transitions/Animations even if they do not follow the ngAnimate CSS naming structure. Upon animation ngAnimate will apply supplementary CSS classes to track the start and end of an animation, but this will not hinder any pre-existing CSS transitions already on the element. To get an idea of what happens during a class-based animation, be sure to view the step by step details of $animate.addClass and $animate.removeClass.

Click here to learn more about the steps involved in the animation.

## Examples

### Basic

    <p ng-class="{strike: deleted, bold: important, 'has-error': error}">Map Syntax Example</p>
    <label>
       <input type="checkbox" ng-model="deleted">
       deleted (apply "strike" class)
    </label><br>
    <label>
       <input type="checkbox" ng-model="important">
       important (apply "bold" class)
    </label><br>
    <label>
       <input type="checkbox" ng-model="error">
       error (apply "has-error" class)
    </label>
    <hr>
    <p ng-class="style">Using String Syntax</p>
    <input type="text" ng-model="style"
           placeholder="Type: bold strike red" aria-label="Type: bold strike red">
    <hr>
    <p ng-class="[style1, style2, style3]">Using Array Syntax</p>
    <input ng-model="style1"
           placeholder="Type: bold, strike or red" aria-label="Type: bold, strike or red"><br>
    <input ng-model="style2"
           placeholder="Type: bold, strike or red" aria-label="Type: bold, strike or red 2"><br>
    <input ng-model="style3"
           placeholder="Type: bold, strike or red" aria-label="Type: bold, strike or red 3"><br>
    <hr>
    <p ng-class="[style4, {orange: warning}]">Using Array and Map Syntax</p>
    <input ng-model="style4" placeholder="Type: bold, strike" aria-label="Type: bold, strike"><br>
    <label><input type="checkbox" ng-model="warning"> warning (apply "orange" class)</label>

    .strike {
        text-decoration: line-through;
    }
    .bold {
        font-weight: bold;
    }
    .red {
        color: red;
    }
    .has-error {
        color: red;
        background-color: yellow;
    }
    .orange {
        color: orange;
    }

    var ps = element.all(by.css('p'));

    it('should let you toggle the class', function() {

      expect(ps.first().getAttribute('class')).not.toMatch(/bold/);
      expect(ps.first().getAttribute('class')).not.toMatch(/has-error/);

      element(by.model('important')).click();
      expect(ps.first().getAttribute('class')).toMatch(/bold/);

      element(by.model('error')).click();
      expect(ps.first().getAttribute('class')).toMatch(/has-error/);
    });

    it('should let you toggle string example', function() {
      expect(ps.get(1).getAttribute('class')).toBe('');
      element(by.model('style')).clear();
      element(by.model('style')).sendKeys('red');
      expect(ps.get(1).getAttribute('class')).toBe('red');
    });

    it('array example should have 3 classes', function() {
      expect(ps.get(2).getAttribute('class')).toBe('');
      element(by.model('style1')).sendKeys('bold');
      element(by.model('style2')).sendKeys('strike');
      element(by.model('style3')).sendKeys('red');
      expect(ps.get(2).getAttribute('class')).toBe('bold strike red');
    });

    it('array with map example should have 2 classes', function() {
      expect(ps.last().getAttribute('class')).toBe('');
      element(by.model('style4')).sendKeys('bold');
      element(by.model('warning')).click();
      expect(ps.last().getAttribute('class')).toBe('bold orange');
    });

### Animations

The example below demonstrates how to perform animations using ngClass.

    <input id="setbtn" type="button" value="set" ng-click="myVar='my-class'">
    <input id="clearbtn" type="button" value="clear" ng-click="myVar=''">
    <br>
    <span class="base-class" ng-class="myVar">Sample Text</span>

    .base-class {
      transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
    }

    .base-class.my-class {
      color: red;
      font-size:3em;
    }

    it('should check ng-class', function() {
      expect(element(by.css('.base-class')).getAttribute('class')).not.
        toMatch(/my-class/);

      element(by.id('setbtn')).click();

      expect(element(by.css('.base-class')).getAttribute('class')).
        toMatch(/my-class/);

      element(by.id('clearbtn')).click();

      expect(element(by.css('.base-class')).getAttribute('class')).not.
        toMatch(/my-class/);
    });