簡體   English   中英

在離子警報中從按鈕觸發ngSubmit

[英]Firing ngSubmit from button in ionic alert

我已經構建了一個離子應用程序,我在Angular中使用模板驅動的表單來收集表單數據並使用ngSubmit將數據傳遞給ts.file 我想在警報中通過'否並保存數據'按鈕觸發ngSubmit功能但我的警報功能與我的html.file 我不知道如何將數據傳遞給警報功能,要么通過警報觸發ngSubmit

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();
  }

嘗試通過@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();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM