Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49121

Restoring the value of a check button in change event code

$
0
0

@cmilanesi wrote:

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

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>