coldistric
coldistric

Reputation: 303

How can I automatically recheck a checkbox that has been unchecked using angular

I have a checkbox that a user can uncheck or check. when the user unchecks it, I want to automatically check it using angular

I'm using angular 7.

template code:

  type="checkbox"
  [(ngModel)]="CheckBoxValue"
  (ngModelChange)="onChangeCheckBox(CheckBoxValue)"/>

component event to recheck if unchecked:

  public onChangeCheckBox(value: boolean): void {
    if (!value) {
      this._checkBoxValue = true;
      console.log("I have been there");
    }
  }

here s link to my code: https://stackblitz.com/edit/angular-tgud2c

You may Ask what do I want to do this? My original question was more complex. I have tried my best to simplify it. You can find it here

Upvotes: 0

Views: 244

Answers (1)

malifa
malifa

Reputation: 8165

You better use a radio button, but if you can't for whatever reason, you could use a hacky setTimeout to achieve your goal:

public onChangeCheckBox(value: boolean): void {
    setTimeout(() => {
       this._checkBoxValue = true;
    });
}

Upvotes: 1

Related Questions