简体   繁体   English

我应该在表格上使用Ng-submit或ng-click吗?

[英]Should I use Ng-submit or ng-click on a form?

I would like to know if there is a preference to using ng-click on a submit button or just having a ng-submit on a form? 我想知道是否有使用ng-click提交按钮还是仅在表单上进行ng-submit的偏好?

My assumption is that we should use ng-submit on all forms and use ng-click on anything that isn't a form. 我的假设是,我们应该在所有表单上使用ng-submit,并在所有非表单上使用ng-click。

I know the differences that are listed below: 我知道下面列出的区别:

  • ng-submit allows the user to press enter when focused on a form where ng-click doesn't ng-submit允许用户在专注于ng-click不在的表单上时按Enter
  • ng-click can be used on any element where as ng-submit only can be applied to a form element. ng-click可用于任何元素,其中ng-submit仅可应用于表单元素。

I was wondering if anyone can add anything else to this list as I would like to know what the norm is in a angular project. 我想知道是否有人可以在此列表中添加其他内容,因为我想知道角度项目中的规范。

If we want the form not to be submitted when it is invalid, then instead of ng-click on the button, we will use ng-submit directive on the form itself 如果我们希望表单在无效时不提交,那么我们将对表单本身使用ng-submit指令,而不是ng单击按钮。

     <div class="row">
            <form name="adduser" ng-submit="AddUser(adduser.$valid)">                   
                <div id="name-group" class="form-group-lg">
                    <input type="text"
                           required
                           name="name"
                           ng-model="userfullName"
                           class="form-control"
                           placeholder="Full Name">
                </div>

In the ng-submit we calling a function AddUser from the controller with a parameter formname.$valid. 在ng-submit中,我们从控制器使用参数formname。$ valid调用函数AddUser。 This submit function will be only called when form is valid or in other words all the user input of the form is valid. 仅当表单有效或换句话说,表单的所有用户输入均有效时,才会调用此Submit函数。 Keep in mind that in this case from would not be submitted if form is not valid 请记住,在这种情况下,如果表单无效,则不会提交from

When we use ng-click , form will be submitted even if it is invalid. 当我们使用ng-click时,即使表单无效也将被提交。 Two observations for ng-click are as follows: ng-click的两个观察结果如下:

  1. We were able to submit the form even if form was not valid 即使表格无效,我们也可以提交表格
  2. For the invalid inputs, the value was set to undefined in the controller 对于无效输入,该值已在控制器中设置为undefined

Out of experience, for mobile apps using Angular and Ionic , I used ng-click because even when the form isn't valid, the mobile keyboard arrow ( Android ) or GO ( iOS ) is enabled and will still submit it anyway. 根据经验,对于使用AngularIonic移动应用程序,我使用ng-click是因为即使表单无效,也会启用移动键盘箭头( Android )或GO( iOS ),并且无论如何仍会提交。 I had to use ng-click to avoid validation problems. 我不得不使用ng-click来避免验证问题。

On web, I use ng-submit simply because, like you said, it submits on enter. 在网络上,我之所以使用ng-submit仅仅是因为,就像您说的那样,它是在回车时提交的。

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

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