Good day! Understanding Angular 2, it was necessary to display a list of items on the user's screen.
<campaigns> <campaign *ngFor="let campaign of campaigns"></campaign> </campaigns> And it is necessary that each non-even element changes its color from white to gray.
Previously, with simple layout, I used the pseudo-class: nth-of-type (odd). Who himself made every odd element I need colors (for example). But for components this is for some reason not working.
I can certainly do:
<campaigns> <campaign *ngFor="let campaign of campaigns; let i = index" [background]="i % 2 == 0 'green':'white'"></campaign> </campaigns> But it seems to me that this approach is a crutch.
In this connection, the question is how to transfer information from the css parent container inside the component.
Css campaigns code
campaign:nth-of-type(odd) { background: #f2f2f2; } Сss and html campaign code
<div class="campaign"> <div class="inlineBlock videoContainer"> <video width="100%" controls="controls" preload="metadata"> <source src="http://a-media.pro/videos/1479360662.mp4"> </video> </div> <div class="inlineBlock leftContainer"> <a class="campaignName text-top" [routerLink]="['/campaigns', campaign.id]">{{campaign.name}}</a><br/> Показов по плану - {{campaign.countShow}}<br/> Комплексов - {{campaign.countDevice}} <div class="bottom">Тип проката <span class="text-bottom">«{{campaign.methodName}}»</span></div> </div> <img src="/images/line.png" class="inlineBlock" style="margin-top: 8px"> <div class="inlineBlock rightContainer"> <div class="text-top"> {{campaign.budget}} р. </div> c {{campaign.start | date: 'dd.My' }}<br/> до {{campaign.end | date: 'dd.My' }} <div class="bottom right"><span class="text-bottom">{{campaign.statusName}}</span></div> </div> .campaign { width: 100%; height: 190px; padding: 17px 0; color: #000000; font-size: 16px; font-family: "Segoe UI", sans-serif; -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ -moz-box-sizing: border-box; /* Firefox, other Gecko */ box-sizing: border-box; /* Opera/IE 8+ */ }