Vishesh Pandita
Vishesh Pandita

Reputation: 13

Submit in Angular form

I am working on Angular 7 forms. I made a whole form with all fields and just added a simple button at the end of this form and now I am wondering how the click on this button is firing the submit action. I have not even set the attribute type="submit" for this button. I have

(ngSubmit)="onSubmit(employeeForm)" 

at the top of my form. Can someone make me understand how any random button is firing this submit functionality. Thanks already.

P.S. I am using Material Design controls in this form.

Upvotes: 0

Views: 317

Answers (2)

Bhagwat Tupe
Bhagwat Tupe

Reputation: 1943

If you have not declared button type then most browsers by default type of button is submit . Suppose you have create button like
<button name="Save">Save</button> its by default button type is submit.
so that's why you are able to fire submit event within form tag

In IE8 Standards mode, the default value is submit. In other compatibility modes and earlier versions of Windows Internet Explorer, the default value is button.

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68665

button in html has two types - submit and button. If you have a form and inside it a button which type is submit, clicking it will force the form to submit. So if you don't want to submit, you need to change the type of your button to button.

<button mat-button class="btn btn-primary btn-sm" type="button">Save and Next</button>

Upvotes: 4

Related Questions