I'm trying to submit my reactive form from a button outside tags. Before, this form was Template-driven and I could do:
<button (click)="f.ngSubmit.emit()"></button>
if 'f' was form name.
But if I use this approach with model-drive form an error is shown:
TypeError: Cannot read property 'emit' of undefined
I can simulate this behaviour writing a hidden button inside form and setting click event for outside button:
<form [formGroup]="loginForm" (ngSubmit)="login()">
...
<button #submit type="submit" style="visibility:hidden"></button>
</form>
<button (click)="submit.click"></button>
It doesn't seem to be the best approach... Is there another better way?
Thank you.
EDIT:
If I add an id field to the form works, but I really think that isn't the best approach...
<form id="loginF" [formGroup]="loginForm" (ngSubmit)="login()">
...
</form>
<button form="loginF" (click)="submit.click"></button>