Ionic 2:在从其他项目中删除类时向类添加类

时间:2017-07-26 22:21:42

标签: javascript html css angular ionic-framework

我有一个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;
}

enter image description here =&gt; enter image description here

1 个答案:

答案 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}"