0

i have this alertCtrl and i want to use it as a service, my idea is send 3 params to this alert, header, text and a function, this function will be fired when the user press the Ok button, i tryid to send params like this but this doesnt work.

this is my code:

 async showMessage(header,message, function){
  let alert = await this.alertCtrl.create(
  {
    header: header,
    message: message,
    buttons: [
      {
        text: 'Cancel'
      },
      {
        text: 'Ok',
        handler: () => {
          function // i  want to fire a function here
        }
      }
    ]
  });
  await alert.present();
  }

thanks in advance.

greetings

Jordan Quiroz
  • 95
  • 2
  • 10

2 Answers2

0

You are not executing the function inside the handler try adding () like

function();
Renz
  • 51
  • 4
0

Firstly, the word function is a keyword, and should not be used unless you are declaring a function. In the modified code below, I have used onOkClick as the call back function, which you need to attach to the handler.

Secondly, you need not necessarily have 2 awaits unless you have 2 separate parallel events happening which needed to be combined. Even in that case, you will need something like Promise.all where you wait for the 2 async events to complete and then resolve them as one single entity. More info

Try the below code and it should work.

async showMessage(header, message, onOkClick) {

  let alert = this.alertCtrl.create({
      header: header,
      message: message,
      buttons: [{
          text: 'Cancel'
        }, {
          text: 'Ok',
          handler: onOkClick // attach callback
        ];
      });
  }

  await alert.present();

}
Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48