Pv-Viana
Pv-Viana

Reputation: 642

ionic 3 - show confirm alert after click on ion-toggle

I'm trying to use click event on ion-toggle and is not working.

Html:

  <ion-item>
     <ion-label class="labelToggle">Ativo:</ion-label>
     <ion-toggle (click)="mudarStatusProcesso()" [(ngModel)]="ProcAtivo"></ion-toggle>
  </ion-item>

The mudarStatusProcesso() creates an AlertController with 'ok' and 'cancel' options, this action needs happen before ion-toggle updating. I believe that i should use other prop instead "(click)", can anyone help me?

Upvotes: 3

Views: 1758

Answers (2)

Judson Terrell
Judson Terrell

Reputation: 4306

For anyone needing a more updated answer (ionic 4+). I am using the (click)="banUnban($event)" We stop propagation etc until they make a choice then set the value accordingly...

async banUnban(ev: any){
// prevent checking until we confirm whay they want to do...
ev.stopImmediatePropagation();
ev.stopPropagation();
ev.preventDefault();
const message = ev.target.checked ? 'Are you sure you want to ban this user?' : 'Are you sure you want to unban this user?';
const options = {
  header: 'Please confirm.',
  message,
  okHandler: ()=>{
    this.user.is_blocked= !this.user.is_blocked;
    if(this.user.is_blocked){
      console.log('Ban the user...');
    }else{
      console.log('Unban the user...');
    }
  },
  cancelHandler: () => {
    console.log('Nothing to see here...');
  }
} as IAlertOptions;
const confirm = await this._alertService.showConfirm(options);
confirm.present();
}

Upvotes: 0

Mystery
Mystery

Reputation: 1031

You can use (ngModelChange),

<ion-item>
   <ion-label class="labelToggle">Ativo:</ion-label>
   <ion-toggle [(ngModel)]="ProcActivo" (ngModelChange)="mudarStatusProcesso()"></ion-toggle>
</ion-item>

In your .ts,

ProcActivo: boolean = false; // default value

mudarStatusProcesso(){

    let alert = this.alertCtrl.create({
      title: null,
      message: "Confirm?",
      buttons: [{
        text: 'Cancel',
        role: 'cancel',
        handler: () => {
          if(this.ProcActivo==false){
            this.ProcActivo=true;
          }else{
            this.ProcActivo=false;
          }
        }
      },
      {
        text: 'Okay',
        handler: () => {

        }
      }
    ]
  });
  alert.present();

  }

Upvotes: 2

Related Questions