如何将类添加到Angular的ngFor中间元素或默认类

时间:2018-04-29 00:01:13

标签: angular

我通过一些数据进行Angular循环,我可以在第一个和最后一个项目中添加一个类,但是如何向所有其他项添加一个类?

    <div *ngFor="let cat of item['catagory']; 
                 let first = first; 
                 let last = last" 
                 [class.article_cat_first]="first" 
                 [class.article_cat_last]="last">
         {{cat['title']}}
    </div>

或者,是否可以添加默认值?

1 个答案:

答案 0 :(得分:2)

您可以使用条件!first && !last

将类添加到其他项目
<div *ngFor="let cat of item['catagory']; let first = first; let last = last" 
     [class.article_cat_others]="!first && !last" ... >
     ...
</div>

要设置默认类,请使用普通class属性:

<div *ngFor="let cat of item['catagory']; let first = first; let last = last" 
     class="defaultClass" ... >
     ...
</div>

请参阅this stackblitz了解演示。