在Angular 2中提交vs ngSubmit [英] submit vs ngSubmit in Angular 2

查看:66
本文介绍了在Angular 2中提交vs ngSubmit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Angular 2中提交表单,我们可以使用表单的"submit"或"ngSubmit"事件.

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 ?

推荐答案

但是,ngSubmit确保在提交表单时不提交表单 处理程序代码抛出(这是Submit的默认行为),并且 导致实际的http发布请求.让我们使用ngSubmit代替 是最佳做法:

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:

这篇关于在Angular 2中提交vs ngSubmit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆