如何通过位于表单外部的按钮以编程方式提交Angular(2+)表单? 应该通过单击事件来调用Angular组件中的函数吗?
答案 0 :(得分:2)
我使用此处显示的方法(来自:How to trigger Angular 2 form submit from component?)
NgForm具有属性ngSubmit,它是EventEmitter。所以在做上emit() 组件中的此属性将触发提交。
此外,您还需要使用f变量而不是formElement,因为f 正在引用ngForm。
@ViewChild('f')形式:NgForm;
form.ngSubmit.emit();
答案 1 :(得分:-1)
**<formname>.ngSubmit.emit()** is used to submit the form when click the button from outside of the form.
For Example :
<form (ngSubmit)="save()" #submitForm="ngForm">
<input type="text" name="name" required>
</form>
<button (click)="submitForm.ngSubmit.emit()"
[disabled]="!submitForm.form.valid">SAVE</button>
When click the SAVE button its call the save() function in the component.