Submit a form from a button outside of the form in Angular

Viewed 3045

How can I submit a form in Angular (2+) programmatically from a button which resides outside of the form? This should be through a click event which invokes a function in an Angular component?

1 Answers

I use the method shown here (from: How to trigger Angular 2 form submit from component?)

NgForm has property ngSubmit which is EventEmitter. So doing emit() on this property from the component will trigger a submit.

Also you need to use your f variable instead of formElement because f is referencing to ngForm.

@ViewChild('f') form: NgForm;

form.ngSubmit.emit();

Related