I have an angular reactive form
<form [formGroup]="form" (ngSubmit)="onSubmit()">
I have two buttons to submit. I need to perform a common operation when users press the button, that is submit the form, but also I need to differentiate between the buttons, because I need to redirect the user to different pages, depending on the button pressed. Here is my two buttons:
<button name="Previous" type="submit" [disabled]="form.invalid"> Previous</button>
<button name="Next" type="submit" [disabled]="form.invalid">Next</button>
How can I know in the OnSubmit event which button was pressed?
You can try with this solution
component.html
<form [formGroup]="form" (ngSubmit)="onSubmit(buttonType)">
<button type="submit" (click)="onSubmit('Next')">Next</button>
<button type="button" (click)="onSubmit('Previous')">Previous</button>
</form>
component.ts
onSubmit(buttonType): void {
if(buttonType==="Next") {
console.log(buttonType)
}
if(buttonType==="Previous"){
console.log(buttonType)
}
}