Restoring the value of a check button in change event code


#1

I want to use a check button to do an operation that can fail. If it fails, the check button value should be restored.
I have this HTML code:

<ion-checkbox [(ngModel)]="isChecked" (ionChange)="onChange()">
</ion-checkbox>

and this TypeScript code:

    private isChecked = false;
    private changedByCode = false;
    private onChange() {
        if (this.changedByCode) {
            this.changedByCode = false;
            console.log('AUX', this.isChecked);
            return;
        }
        console.log('before:', this.isChecked);
        // Try an operation (omitted), if it fails, do the following.
        this.changedByCode = true;
        this.isChecked = ! this.isChecked;
        console.log('after:', this.isChecked);
    }

The use of changedByCode is needed to avoid endless iteration.
The first time the user clicks the check button, correctly the button blinks and remains unchecked, and on the console the following appears:

before: true
after: false
AUX: false

But the second time, the button becomes checked, and the following appears on the console:

before: true
after: false