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