我通过一些数据进行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>
或者,是否可以添加默认值?
答案 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了解演示。