繁体   English   中英

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

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

我是AngularJS的新手,现在正在学习这个todo-mvc示例 让我感到困惑的是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> 

这是addToDo()函数的代码:

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

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

    $scope.newTodo = '';
};

如您所见,没有地方可以显示ng-submit应该对应的事件。 我猜想ng-submit旨在处理input字段的enter事件。 但是,我在官方文档中找不到相关信息。 那么, ng-submit的默认行为是什么? 即,我想知道在各种情况下哪些事件对应于“提交”。 在此特定示例中,“提交”是否意味着输入了输入字段?

非常感谢你。

这写在您作为链接放置的文档的顶部:

使绑定角度表达式绑定到onsubmit事件。

----------- -------------更新

在下面的评论中,您持有:

单击提交按钮时发生onsubmit事件

那是不正确的。

首先,让我们确定ngSubmit指令引发的事件。
angular-scenario.js ,有以下代码:

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

此外,如果您参考HTML 2.0规范,请参阅有关Form Submission的内容 (因此, submit事件):

当表单中只有一个单行文本输入字段时,用户代理应接受该字段中的Enter作为提交表单的请求。

另外,从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