在离子警报中从按钮触发ngSubmit

时间:2019-04-09 14:35:12

标签: angular typescript ionic-framework alert ng-submit

我已经构建了一个离子应用程序,并且在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();
  }

1 个答案:

答案 0 :(得分:0)

尝试通过@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();

}