Используя ngFor для перебора массива элементов, я хочу, чтобы элементы с индексом EVEN имели цвет фона, отличный от элементов с индексом ODD. я смог добиться этого в VueJs, используя приведенный ниже код:
Я пробовал угловой код ниже, без успеха:
<ion-col *ngFor = "let item of [].constructor(50); let i = index" >
<ion-card [attr.color] = "{'secondary': i % 2, 'primary': !(i % 2)}">
</ion-card>
</ion-col>
Код VueJs: этот код работает для меня в VueJs, но мне нужно добиться той же логики в Angular 7
<div v-for = "(itemforsale, index) in filteredListMainitemsforsale">
<div :class = "{'box bg-success text-center': index % 2, 'box bg-info text-center': !(index % 2)}" >
</div>
</div>
вы можете использовать в *ngFor "let event=even" см. angular.io/api/common/NgForOf и ngClass [ngClass] = "{even? 'text-center box bg-success':'text-center bg-info'}" или [ngClass] = "{'text-center box bg-success':even,'text-center bg-info'}:!even"





<ion-col *ngFor = "let item of [].constructor(50); let i = index" >
<ion-card [style.color] = "i % 2 === 0 ? 'secondary' : 'primary'">
</ion-card>
</ion-col>
Попробуйте так:
<ion-card [style.color] = "i % 2?'secondary': 'primary'">
Для цвета фона:
<ion-card [style.background-color] = "i % 2?'secondary': 'primary'">
вы можете попробовать вот так
<ion-col *ngFor = "let item of [].constructor(50); let i = index" >
<ion-card [ngStyle] = "{'color': i % 2 == 0 ? 'secondary' : 'primary'}">
</ion-card>
</ion-col>
Что-то такое? https://stackblitz.com/edit/angular-fia7hx
<div *ngFor = "let item of [1,2,3,4,5,6,7,8,9]; index as i">
<p [ngClass] = "{greyClass: i%2 === 0}">
{{item}}
</p>
</div>
.greyClass {
background-color: grey;
}
просто используйте четное и нечетное свойство css