我有这段代码:
<thead>
<th class="dude" *ngFor="let tp of column_names;let i=index">{{tp}}</th>
</thead>
如何根据索引应用类?例如。当其余的得到dude_1时,第一个索引会获得类dude_0吗?
e.g。
<thead>
if i==0
<th class="dude_0" *ngFor="let tp of column_names;let i==index"{{tp}}</th>
if i>0
<th class="dude_1" *ngFor="let tp of column_names;let i=index"{{tp}}</th>
</thead>
答案 0 :(得分:3)
<th *ngFor="let tp of column_names; index as index" [class.dude_0]="index === 0"
[class.dude_1]="index > 0">{{tp}}</th>
甚至更简单,第一个:
<th *ngFor="let tp of column_names; first as first" [class.dude_0]="first"
[class.dude_1]="!first">{{tp}}</th>
答案 1 :(得分:2)
使用[ngClass]
<thead>
<th [ngClass]="{'dude_0': i==0, 'dude_1': i>0}" *ngFor="let tp of column_names;let i=index">{{tp}}</th>
</thead>