I've tried to implement all the recommended methods as suggested
Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError
error
ExpressionChangedAfterItHasBeenCheckedError Explained
However, either it's in combination with Ionic or my own developer skills that are letting me down, I am unable to get this to work without hitting the ExpressionChangedAfterItHasBeenCheckedError error.
I have a list of items that I would like to be able to select independently or allow the user to Select All.
In the .ts file I have two methods:
toggleSelectedUser(user) {
const index = this.selectedUsers.indexOf(user);
if (index >= 0) {
this.selectedUsers.splice(index, 1);
} else {
this.selectedUsers.push(user);
}
}
toggleSelectAll() {
if (!this.selectedUsers.length) {
this.selectedUsers = [...this.template.userData];
} else {
this.selectedUsers = [];
}
}
And in my template I am using
<ion-toggle item-end color="secondary" checked="false" id="checkbox{{idx}}" [checked]="isSelected(user)" (ionChange)="toggleSelectedUser(user)"></ion-toggle>
Is selected simply does this:
isSelected(user) {
return this.selectedUsers.indexOf(user) >= 0;
}
Single toggles are working fine, however when I try to perform toggleSelectAll()
I get the dreaded ExpressionChangedAfterItHasBeenCheckedError
.