Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign alternate class to rows in Angular JS?

I want to assign alternate class to rows in a table. I am using ng-repeat on

<tr ng-repeat="event in events"> 

I want to get output like this:

<tr class="odd">...</tr> <tr class="event">....</tr> 

I've tried this (doesn't work):

<tr ng-repeat="event in events" class="$index % 2 == 0? 'event' : 'odd'"> 

I can't get it to work. Also it seems like Angular is using 'class' attribute to. Why is it doing so? Can I tell AngularJS not to use the class attribute for internal evaluations?

Please help. Thanks!

like image 277
S Gupta Avatar asked Aug 29 '12 13:08

S Gupta


People also ask

How to add alternate row colors in table using AngularJS?

For example by using ngClassOdd,ngClassEven directives we gonna add alternate row colors in table. First create css, for applying different color to each type, blow is the css. The ng-app directive initializes an AngularJS application.

What is ngclass in angular?

ngClass is a directive in Angular that adds and removes CSS classes on an HTML element. In this article, we are talking about ngClass in Angular only, not ng-class in angular.js. Prerequisites – What is Property Binding? Two things we have to understand first are property binding and interpolation in Angular.

How to set class(es) for all rows in CSS?

CSS class (es) for all rows. Provide either a string (class name) or array of strings (array of class names). Callback version of property rowClass to set class (es) for each row individually. Function should return either a string (class name), array of strings (array of class names) or undefined for no class.

How to create a Bootstrap component in angular?

Choose the folder and open it. Now it's time to create the component for the project, so open the terminal and type the following command, First, we will add the reference for bootstrap and toaster in the angular.json file. Type the following command to generate the model file for the class. We're all done with installation and project setup.


Video Answer


2 Answers

You should be using the angular directives ngClassEven and ngClassOdd for this.

Have a look at the documentation section for how to use them

http://docs.angularjs.org/api/ng.directive:ngClassEven

http://docs.angularjs.org/api/ng.directive:ngClassOdd

Hope this helps.

like image 104
ganaraj Avatar answered Oct 17 '22 21:10

ganaraj


From the Angular docs..

Use ng-class-odd ng-class-even

<li ng-repeat="name in names">   <span ng-class-odd="'odd'" ng-class-even="'even'">     {{name}}   </span> </li> 
like image 44
Fintan Kearney Avatar answered Oct 17 '22 21:10

Fintan Kearney