首页 > 解决方案 > 从离子警报中的按钮触发 ngSubmit

问题描述

我已经构建了一个离子应用程序,并在 Angular 中使用模板驱动的表单来收集表单数据并用于ngSubmit将数据传递到ts.file. 我想ngSubmit通过警报中的“否并保存数据”按钮触发功能,但我的警报功能与我的html.file. 我不知道如何将数据传递给警报功能,要么ngSubmit通过警报触发。

html.文件

<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.文件

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

标签: angulartypescriptionic-frameworkalertng-submit

解决方案


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

}


推荐阅读