简体   繁体   English

在Angular 2中提交vs ngSubmit

[英]submit vs ngSubmit in Angular 2

In order to submit a form in Angular 2 we can either use form's "submit" or "ngSubmit" event. 为了在Angular 2中提交表单,我们可以使用表单的“ submit”或“ ngSubmit”事件。

<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 ? 想知道两者之间的区别是什么?

from this tutorial, https://blog.thoughtram.io/angular/2016/03/21/template-driven-forms-in-angular-2.html 从本教程中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. 但是,ngSubmit确保在处理程序代码抛出(提交的默认行为)并导致实际的http发布请求时不提交表单。 Let's use ngSubmit instead as this is the best practice: 让我们改用ngSubmit,因为这是最佳做法:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM