l.gloria
l.gloria

Reputation: 1

Firing ngSubmit from button in ionic alert

I've built an ionic app and I use template driven form in Angular to collect form data and use ngSubmit to pass data to ts.file. I want to fire ngSubmit function through 'No and save data' button in alert but my alert function has nothing to do with my html.file. I don't know how to pass data to alert function, either fire ngSubmit through alert.

html.file

<form #observeForm="ngForm" (ngSubmit)="onSubmit(observeForm.value)" [(observeForm.value)]="tester">
...
  <ion-button margin-top="auto" expand="block" type="submit" routerDirection="backforward">Confirm</ion-button>
</form>

ts.file

  async presentAlertConfirm() {
    const alert = await this.alertController.create({
      header: 'Time Out!',
      message: 'Do you want to add more observe time?',
      buttons: [
        {
          text: 'Yes',
          cssClass: 'secondary',
          handler: () => {
            this.startTimer();
            console.log('Add time Okay');
          }
        },
          {
          text: 'No and save data',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            this.timer = 0;
          }
        }
      ]
    });
    await alert.present();
  }

Upvotes: 0

Views: 582

Answers (1)

Roman Šim&#237;k
Roman Šim&#237;k

Reputation: 900

Try to get reference by @ViewChild():

@ViewChild('observeForm') obsForm: any; // new line

async presentAlertConfirm() {
const alert = await this.alertController.create({
  header: 'Time Out!',
  message: 'Do you want to add more observe time?',
  buttons: [
    {
      text: 'Yes',
      cssClass: 'secondary',
      handler: () => {
        this.startTimer();
        console.log('Add time Okay');
      }
    },
    {
      text: 'No and save data',
      role: 'cancel',
      cssClass: 'secondary',
      handler: (blah) => {
        this.obsForm.onSubmit(); // new line
        this.timer = 0;
      }
    }
  ]
});
await alert.present();

}

Upvotes: 0

Related Questions