Add array to ngclass angular 2

I am trying to add an array of classes to a string. my problem is that this is line duplication. so my question is how can I loop on the arrow inside [ngClass]

my code that doesn't work.

.ts

 @Input() rowClasses = ['someClass1', 'someClass2'] 

HTML

 <table> <thead> <tr> <th>some head</th> </tr> </thead> <tbody> <tr class="striped-row" *ngFor="class of rowClasses" [ngClass]="class"></tr> </tbody> </table> 
+6
source share
3 answers

You don't need a for loop, which you just need to assign to your ngClass array Example:

 [ngClass]="rowClasses" 

or

 [ngClass]="['someClass1', 'someClass2']" 

helpful answers and documentation

+3
source

class property should not transfer your class to {} , just specify the binding of the class to [ngClass] properties. In this case, you can do [ngClass]="class"

Your question seems unclear, which does not explicitly indicate where you wanted to apply the class on each row / class for different row

If these classes should appear on one line, then just pass the rowClasses directive to [ngClass]

 [ngClass]="rowClasses" 
0
source

You loop into tr to duplicate html. You don't need to quote css classes

 [ngClass]="rowClasses" //<-- assuming `rowClasses` is an array 

Example usage <some-element [ngClass]="['first', 'second']">...</some-element>

check the documentation for other ways to add classes https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html

0
source

Source: https://habr.com/ru/post/1013097/


All Articles