【发布时间】:2020-04-16 14:28:07
【问题描述】:
我有一堆由 ngFor 循环生成的离子芯片。我使用tagDefaultColor 变量添加了选择所有离子芯片功能。现在当我想选择单个离子芯片时,它会选择所有的。
我想要实现的是能够使用切换所有按钮来选择每个ion-chip 或通过点击事件一一选择它们。一旦选择了ion-chip,它的颜色就会变为主要颜色。提前谢谢你。
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.information = navParams.data.data;
this.children = [{ id: 1, name: 'Ginny Weasley' }, { id: 2, name: 'Harry Potter' }, { id: 3, name: 'Ronald Weasley' }, { id: 4, name: 'Luna Lovegood' }];
this.selectButtonText = 'SELECT ALL';
//this.tagSelectedColor = "primary";
this.tagDefaultColor = "secondary";
this.quantity = 0.0;
this.shareWithFamily = true;
}
selectAll() {
if (this.selectButtonText === 'SELECT ALL') {
this.selectButtonText = 'UNSELECT ALL'
this.tagDefaultColor = "primary";
} else {
this.selectButtonText = 'SELECT ALL'
this.tagDefaultColor = "secondary"
}
}
changeTagColor(event) {
console.log(event.target);
if (this.tagDefaultColor === "secondary") {
this.tagDefaultColor = "primary"
} else {
this.tagDefaultColor = "secondary"
event.target.setAttribute('color', 'secondary')
}
}
HTML 部分
<ion-item no-lines>
<ion-row>
<ion-col style="display: flex;align-items: center; justify-content: center">
<strong>Tag Students</strong>
<button ion-button full color="primary" class="select-all-btn" (click)="selectAll()">{{selectButtonText}}
</button>
</ion-col>
</ion-row>
</ion-item>
<div class="students-tags">
<ion-chip [id]="child.id" [color]="tagDefaultColor" (click)="changeTagColor($event)" *ngFor="let child of children">
<ion-label>{{child. name}}</ion-label>
</ion-chip>
</div>
【问题讨论】: