cssangularrowsalternating

How set css to alternating rows in angular 4


I would like to define a class to alternating rows in angular 4, how to do this?

HTML

 <div class="scrollbars">
            <div  *ngFor="let path of Xyzpaths">
                    <div>{{path.xyz}}</div>
                    <div>{{path.salesItem}}</div>
                    <div>{{path.path1}}</div>
                    <div>{{path.path2}}</div>
            </div>
   </div>

CSS

.odd  {   background-color: #f2f9ff;} 
.even {   background-color: #eceff3; }

Solution

  • yes, you can use the odd and even local variables in the ngFor, something like this:

    <div class="scrollbars">
        <div *ngFor="let path of Xyzpaths index as i; let even = even; let odd = odd" 
         [ngClass]="{ myOddClass: odd, myEvenClass: even }">
            <div>{{path.xyz}}</div>
            <div>{{path.salesItem}}</div>
            <div>{{path.path1}}</div>
            <div>{{path.path2}}</div>
        </div>
    </div>
    

    Documentation.