简体   繁体   English

ng-submit的默认行为是什么?

[英]What is the default behavior of ng-submit?

I have new to AngularJS and am now learning this todo-mvc example . 我是AngularJS的新手,现在正在学习这个todo-mvc示例 One thing confusing to me is the behavior of the ng-submit in the following code from the index.html: 让我感到困惑的是index.html中以下代码中ng-submit的行为:

<form id="todo-form" ng-submit="addTodo()">
    <input id="new-todo" placeholder="What needs to be done?" ng-model="newTodo" autofocus>
</form> 

Here is the code for the addToDo() function: 这是addToDo()函数的代码:

$scope.addTodo = function () {
    var newTodo = $scope.newTodo.trim();
    if (!newTodo.length) {
        return;
    }

    todos.push({
        title: newTodo,
        completed: false
    });

    $scope.newTodo = '';
};

As you can see, there is nowhere that shows what event ng-submit is supposed to correspond to. 如您所见,没有地方可以显示ng-submit应该对应的事件。 I am guessing that ng-submit is meant to handle the enter event of the input field. 我猜想ng-submit旨在处理input字段的enter事件。 However, I can't find relevant information in the official document . 但是,我在官方文档中找不到相关信息。 So, what is the default behavior of ng-submit ? 那么, ng-submit的默认行为是什么? Namely, I would like to know what events correspond to "submit" in various situations. 即,我想知道在各种情况下哪些事件对应于“提交”。 In this particular example, is it true that "submit" means enter of the input field? 在此特定示例中,“提交”是否意味着输入了输入字段?

Thank you very much. 非常感谢你。

This is written at the top of the documentation you put as link: 这写在您作为链接放置的文档的顶部:

Enables binding angular expressions to onsubmit events. 使绑定角度表达式绑定到onsubmit事件。

-----------UPDATE------------- ----------- -------------更新

In your comment below, you held: 在下面的评论中,您持有:

onsubmit event occurs when you click a submit button 单击提交按钮时发生onsubmit事件

That's not exact. 那是不正确的。

First, let's be sure about the event thrown by ngSubmit directive. 首先,让我们确定ngSubmit指令引发的事件。
In the angular-scenario.js , there is this code: angular-scenario.js ,有以下代码:

var ngSubmitDirective = ngDirective(function(scope, element, attrs) {
  element.bind('submit', function() {
    scope.$apply(attrs.ngSubmit);
  });
});

Moreover, if you refers to the HTML 2.0 spec, about the Form Submission , (thus submit event): 此外,如果您参考HTML 2.0规范,请参阅有关Form Submission的内容 (因此, submit事件):

When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form. 当表单中只有一个单行文本输入字段时,用户代理应接受该字段中的Enter作为提交表单的请求。

Also, from Angular's code, line 15527 (depending on current version): 另外,从Angular的代码中,第15527行(取决于当前版本):

* To prevent double execution of the handler, use only one of the {@link ng.directive:ngSubmit ngSubmit}
 * or {@link ng.directive:ngClick ngClick} directives.
 * This is because of the following form submission rules in the HTML specification:
 *
 * - If a form has only one input field then hitting enter in this field triggers form submit
 * (`ngSubmit`)
 * - if a form has 2+ input fields and no buttons or input[type=submit] then hitting enter
 * doesn't trigger submit
 * - if a form has one or more input fields and one or more buttons or input[type=submit] then
 * hitting enter in any of the input fields will trigger the click handler on the *first* button or
 * input[type=submit] (`ngClick`) *and* a submit handler on the enclosing form (`ngSubmit`)

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

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