ngClass, ngClassEven And ngClassOdd Directive In AngularJS

Introduction

AngularJS provides many built-in directives. In this article we will discuss about ngClass, ngClassEven and ngClassOdd directives.

ngClass

This directive allow us to set CSS class to the HTML element dynamically using binding expression representing which classes to be added.

This directive can be used by three ways and it identify by how we pass the expression

1. Pass expression as a string: String should contain one or more space-delimited class names which want to set to the element

Example

HTML

  1. <h5>1) Expression pass as a string.</h5>  
  2. <div ng-controller="HomeController">  
  3.     <div ng-class="className">This is my ngClass Test</div>  
  4.     <input ng-model="className" placeholder="Type: italic bold error" />  
  5.     <br />   
  6. </div>  
CSS class
  1. .italic  
  2. {  
  3.     font - style: italic;  
  4. }.bold  
  5. {  
  6.     font - weight: bold;  
  7. }.red  
  8. {  
  9.     colorred;  
  10. }.error  
  11. {  
  12.     colorred;  
  13. }  
Output

Pass expression as a string


2. Pass expression as an object: Object contains key-value pair. If expression pass as an object corresponding key is used as class name through which object value is set to true.

Example

HTML
  1. <h4> 2) Expression pass as an object.</h4>  
  2. <div ng-controller="HomeController">  
  3.     <div ng-class="{italic: italic, bold: bold, 'error': error}">This is my ngClass Test</div>  
  4.     <br />  
  5.     <label>  
  6.         <input type="checkbox" ng-model="italic"> Apply "italic" class </label>  
  7.     <br>  
  8.     <label>  
  9.         <input type="checkbox" ng-model="bold"> Apply "bold" class </label>  
  10.     <br>  
  11.     <label>  
  12.         <input type="checkbox" ng-model="error"> Apply "Error" class </label>  
  13. </div>  
CSS class is same as previous example.

Output

Pass expression as an object

3. Pass expression as an array: This is mix mode of pass expression as string and object. Array is predefined in length. In the following example, array contains three expressions (classname1, classname2 and classname3) each are bind with textbox. When the value of text box is changed, expression value also change.

Example

HTML
  1. <h5>3) Expression pass as an object.</h5>  
  2. <div ng-controller="HomeController">  
  3.     <div ng-class="[className1,className2,className3]">This is my ngClass Test</div>  
  4.     <input ng-model="className1" placeholder="Class name (1) Type: italic bold error" />  
  5.     <br />  
  6.     <input ng-model="className2" placeholder="Class name (2) Type: italic bold error" />  
  7.     <br />  
  8.     <input ng-model="className3" placeholder="Class name (3) Type: italic bold error" />  
  9.     <br />   
  10. </div>  
CSS class is same as previous example.

Output:

Pass expression as an array

This directive does not add duplicate class if any class was already assigned to the element. When expression is changed, existing applied classes are removed and new classes are applied.

ngClassEven / ngClassOdd

ngClassEven and ngClassodd directives work in conjunction with ngRepeat directive. The behavior is same as ngClass directive. This applied CSS classes to even/odd rows.

Example

HTML
  1. <h4>ngClassEven / ngClassOdd Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <ol>  
  4.         <li ng-repeat="name in names"> <span ng-class-odd="'oddClass'" ng-class-even="'evenClass'">  
  5. {{name}}  
  6. </span> </li>  
  7.     </ol>  
  8. </div>  
Controller
  1. var app = angular.module("app", []);  
  2. app.controller("HomeController", function ($scope)  
  3. {  
  4.     $scope.names = ["Tejas""Jignesh""Rakesh"]  
  5. });  
  6. CSS Class: .oddClass  
  7. {  
  8.     background - colorgray;  
  9. }.evenClass  
  10. {  
  11.     background - colorwhite;  
  12. }  
Output

Output

Summary

The ngClass, ngClassEven and ngClassOdd are important directives to apply CSS classes dynamically in AngularJS. This article will help you to learn all of them.

Up Next
    Ebook Download
    View all
    Learn
    View all