Return value from alert confirmation

Hi,

I created a confirmation alert from one service, then i will call the method from other service. Is there a way to return true or false if yes/no is clicked? so that i will create only 1 confirmation alert. thanks in advance

1 Like

Dismiss the alert manually using alert.dismiss(). At the controller end, use alert.onDidDismiss to accept the data.

In your service file,

promptAlert()
{
    let alert = this.alertCtrl.create({
        title: 'Yes/No',
        buttons: [
            {
                text: 'Yes',
                handler: () => {
                    alert.dismiss(true);
                    return false;
                }
            }, {
                text: 'No',
                handler: () => {
                    alert.dismiss(false);
                    return false;
                }
            }
        ]
    });

    return alert;
}

In your class file,

let alert = this.alert.promptAlert();

alert.present();

alert.onDidDismiss((data) => {
      console.log('Yes/No', data);
});
12 Likes

Thanks for you reply ! I had to test it in a personal project and helped me a lot. However, I’m still wondering why returning false in the case where the Yes button is pressed? Besides, the data passed in the alert.dismiss method is true so I was wondering why returning false.

Thanks again for the reply !

When you return false, the dialog is not dismissed automatically when the user presses the button. This gives you a way to dismiss it yourself within the handler using alert.dismiss(value).

If you returned false, the dismiss would be called internally by AlertController and your alert.dismiss(value) will never be called.

1 Like

In my case, this solution works, but I have to change somethigs:

my service:

async showMessageOkCancel(title, message) {
    let choice
    const alert = await this.alertController.create({
        header: title,
        subHeader: message,
        buttons: [{
            text: 'Sim',
            handler: () => {
                alert.dismiss(true)
                return false
            }
        }, {
            text: 'Não',
            handler: () => {
                alert.dismiss(false);
                return false;
            }
        }]
    });

    await alert.present();
    await alert.onDidDismiss().then((data) => {
        choice = data
    })
    return choice
}

and in the page

this.baseSrv.showMessageOkCancel('Blah?', 'blah blah').then((res) => {
  console.log(res)
})
6 Likes

Usei o async e await em cascata

I use it a little differently.

Service function:

  async presentConfirm(header: any,message: any,cancelText: any,okText: any): Promise<any> {
    return new Promise(async (resolve) => {
      const alert = await this.alertController.create({
        header: header,
        message: message,
        buttons: [
          {
            text: cancelText,
            role: 'cancel',
            cssClass: 'secondary',
            handler: (cancel) => {
              resolve('cancel');
            }
          }, {
            text: okText,
            handler: (ok) => {
              resolve('ok');
            }
          }
        ]
      });
      alert.present();
    });
  }

I use it like this.

    this.myservice.presentConfirm('confirm','confirm deletion','Cancel','OK') .then(res => {
      if (res === 'ok') {
        // codes
      }
    });
8 Likes

thank you so much thats really help me

1 Like

[zbahadir]
Good !!! This is what I was looking for.
regards.

1 Like

great, works fine !!!

Excellent resolution, it worked for me !!! It was exactly what I was looking for. Thanks a lot.

thanks for the code, saved my time!!