我有一个div按钮,点击后,应该添加一个更改按钮颜色的类(selected-date-item
)。在此之前,我想从之前持有该类的任何按钮中删除该类(selected-date-item
)。
CSS
.selected-date-item {
background:#272829;
color:white;
}
HTML
<button class="date-time-select" *ngFor="let chooseDate of possibleDates" (tap)="selectPickupDate(chooseDate)">{{chooseDate}}</button>
JS
selectPickupDate(selectedDate) {
this.selectedDate = selectedDate;
}
答案 0 :(得分:2)
使用[ngClass]
- 指令动态设置CSS类。
<button [ngClass]="{'selected-date-item': chooseDate == selectedDate, 'not-selected-item': chooseDate != selectedDate}" *ngFor="let chooseDate of possibleDates" (tap)="selectPickupDate(chooseDate)">{{chooseDate}}</button>
[ngClass]
突破:
[ngClass]="{'selected-date-item': chooseDate == selectedDate, 'not-selected-item': chooseDate != selectedDate}"