Here is the html code:
<ion-checkbox color="dark" [checked]="selectedCategory.indexOf(category.i_id) >= 0" (ionChange)="updateCheckedOptions(category.i_id,$event)"></ion-checkbox>
Here is the ionViewDidLoad() method:
for (let i = 0; i < vendor.category_id.length; i++) {
this.selectedCategory.push(
vendor.category_id[i].i_vendor_category_id
);
}
and the ionChange event method:
updateCheckedOptions(id) {
let idx = this.selectedCategory.indexOf(id);
if (idx > -1) {
this.selectedCategory.splice(idx, 1);
} else {
this.selectedCategory.push(id);
}
console.dir(this.selectedCategory);
}
Problem: When I am dynamically setting the selectedCategory
array with the values, I get an error:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.
If I comment the updateCheckedOptions
method, I don’t get the error but I am not able to perform my task on ionChange. I need this method to set the array when the user taps on the checkboxes.
How to keep the ionChange method working and not get the above error? Can anybody help?