Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submit vs ngSubmit in Angular 2

Tags:

angular

In order to submit a form in Angular 2 we can either use form's "submit" or "ngSubmit" event.

<form #frm="ngForm" (submit)="add(frm.value)">   ... </form>  <form #frm="ngForm" (ngSubmit)="add(frm.value)">   ... </form> 

Would like to know whats the difference between the two ?

like image 981
refactor Avatar asked Feb 03 '17 04:02

refactor


1 Answers

from this tutorial, https://blog.thoughtram.io/angular/2016/03/21/template-driven-forms-in-angular-2.html

However, ngSubmit ensures that the form doesn’t submit when the handler code throws (which is the default behaviour of submit) and causes an actual http post request. Let’s use ngSubmit instead as this is the best practice:

like image 107
Tiep Phan Avatar answered Oct 11 '22 09:10

Tiep Phan