Angular2:根据点击事件给元素添加ngClass
Angular 2: Adding ngClass to the element based on the click event
我希望 Clicked class 一次只应用于一个被点击的元素。单击其他元素后,第一个单击的元素不应再具有 class。它应该有点像那个特定元素的 this.clicked。
.rangeContainer{
width: 100%;
height: 46px;
}
.range{
height: 42px;
background-color: #F6F6F6;
color: #035688;
font-size: 12px;
font-weight: 800;
line-height: 46px;
padding: 15px 20px;
cursor: pointer;
}
.clicked{
background-color: white;
color: #7A232E;
border-top: 6px solid #7A232E;
}
I want the
<div class="rangeContainer">
<span (click)="click = !click" [ngClass]="{'clicked' : click}" class="range">K4 - K5</span>
<span (click)="click = !click" [ngClass]="{'clicked' : click}"class="range">1ST - 2ND</span>
<span (click)="click = !click" [ngClass]="{'clicked' : click}"class="range">3RD - 4TH</span>
<span (click)="click = !click" [ngClass]="{'clicked' : click}"class="range">5TH - 8TH</span>
</div>
在组件中:
// Insert your real labels here
this.items = [{label: 'item 1'}, {label: 'item 2'}];
然后使用*ngFor
:
<div class="rangeContainer">
<span *ngFor="let item of items" (click)="toggleClick(item)" [ngClass]="{'clicked' :item.isClicked}" class="range">{{item.label}}</span>
</div>
然后像这样切换:
toggleClick(clickedItem: any): void {
for (let item of items) {
item.isClicked = false;
}
clickedItem.isClicked = true;
}
这将确保一次只能点击 1 个项目。我正在使用一个循环,所以我可以将点击状态存储在对象上。它让生活更轻松。
<span
*ngFor="let range of ['K4 - K5', '1ST - 2ND', '3RD - 4TH', '5TH - 8TH']; let i = index"
(click)="activeIndex = i"
[ngClass]="{ clicked : activeIndex === i }" class="range" >
{{ range }}
</span>
我希望 Clicked class 一次只应用于一个被点击的元素。单击其他元素后,第一个单击的元素不应再具有 class。它应该有点像那个特定元素的 this.clicked。
.rangeContainer{
width: 100%;
height: 46px;
}
.range{
height: 42px;
background-color: #F6F6F6;
color: #035688;
font-size: 12px;
font-weight: 800;
line-height: 46px;
padding: 15px 20px;
cursor: pointer;
}
.clicked{
background-color: white;
color: #7A232E;
border-top: 6px solid #7A232E;
}
I want the
<div class="rangeContainer">
<span (click)="click = !click" [ngClass]="{'clicked' : click}" class="range">K4 - K5</span>
<span (click)="click = !click" [ngClass]="{'clicked' : click}"class="range">1ST - 2ND</span>
<span (click)="click = !click" [ngClass]="{'clicked' : click}"class="range">3RD - 4TH</span>
<span (click)="click = !click" [ngClass]="{'clicked' : click}"class="range">5TH - 8TH</span>
</div>
在组件中:
// Insert your real labels here
this.items = [{label: 'item 1'}, {label: 'item 2'}];
然后使用*ngFor
:
<div class="rangeContainer">
<span *ngFor="let item of items" (click)="toggleClick(item)" [ngClass]="{'clicked' :item.isClicked}" class="range">{{item.label}}</span>
</div>
然后像这样切换:
toggleClick(clickedItem: any): void {
for (let item of items) {
item.isClicked = false;
}
clickedItem.isClicked = true;
}
这将确保一次只能点击 1 个项目。我正在使用一个循环,所以我可以将点击状态存储在对象上。它让生活更轻松。
<span
*ngFor="let range of ['K4 - K5', '1ST - 2ND', '3RD - 4TH', '5TH - 8TH']; let i = index"
(click)="activeIndex = i"
[ngClass]="{ clicked : activeIndex === i }" class="range" >
{{ range }}
</span>