Angular – 以编程方式提交表单.
我在HTML上有一个表单组,我希望组件使用post方法中的电子邮件字段提交表单的操作.而不是使用正常的提交按钮.
下面的testMethod从另一个按钮调用.在这个方法中,我想发布testForm.它必须以旧学校的方式发布,因为它需要一个动作.
这是我的HTML:
<form [formGroup]="testGroup" [action]='actionLink' method='POST' #testForm> <input name='Email' type='hidden' [value]='currentUserEmail'> </form>
这是我的Component TS文件尝试:
@ViewChild('testForm') testFormElement; public currentUserEmail: string = ''; public testGroup = this.formBuilder.group({ Email: '' }); public testMethod(): void { // Below: This currently doesnt seem to do anything. this.testFormElement.ngSubmit.emit(); }
解决方法
import { Component,ViewChild } from '@angular/core'; @Component({ template: ` <form #testForm> <input name='Email' type='hidden'> </form> ` }) class MyComponent { @ViewChild('testForm') testFormEl; testMethod() { this.testFormEl.nativeElement.submit() } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。