Angular ng-submit调用了两次 [英] Angular ng-submit called twice

查看:157
本文介绍了Angular ng-submit调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个角度形式,其提交方法被击中两次,我无法弄清楚为什么。我对Angular很新,所以我可能会忽略一些相当简单的东西...

So, I have an angular form whose submit method is being hit twice, and I can't figure out why. I'm pretty new to Angular, so it's possible I'm overlooking something fairly simple...

Html:

<div ng-app="RegistrationApp" ng-controller="RegistrationController">
    <form name="accountForm" ng-submit="submitAccount($event, accountForm, account)"  novalidate>

        // inputs here...

        <button type="submit" class="btn btn-success pull-right" ng-disabled="accountForm.$invalid">Submit</button>
    </form>
</div>

Js:

var RegistrationApp = angular.module('RegistrationApp', []);

RegistrationApp.controller('RegistrationController', function ($scope) {

    $scope.submitAccount = function (evt, form, account) {
        console.log('submitAccount() hit'); 
        console.log(evt);
        console.log(form);

        evt.stopPropagation();

        // AJAX code
    });
});

控制台窗口:

submitAccount() hit 
o.Event {originalEvent: Event, type: "submit", isDefaultPrevented: function, timeStamp: 1394139847226, jQuery210012237170152366161: true…}
c {$error: Object, $name: "accountForm", $dirty: true, $pristine: false, $valid: true…}

submitAccount() hit 
o.Event {originalEvent: Event, type: "submit", isDefaultPrevented: function, timeStamp: 1394139847226, jQuery210012237170152366161: true…}
Constructor {$error: Object, $name: "accountForm", $dirty: true, $pristine: false, $valid: true…}

所以,我尝试的第一件事就是停止传播这个事件,但那没有任何真实的影响。在浏览事件对象后,它们看起来完全相同。唯一不同的是'形式'对象。属性是相同的,除了一个显示c,另一个显示构造函数。

So, the first thing I tried was to stop propogating the event, but that doesnt have any real effect. After going through the event objects, they're seem identical. The only thing that differs is the 'form' object. The properties are the same, except that one shows that "c" and the other shows "Constructor".

任何可能导致此触发两次的想法?在两种情况下,事件目标都设置为表单元素,并且我没有使用任何onclick函数或表单中的任何其他类型的事件。

Any ideas what could be causing this to trigger twice?? The event target is set to the form element in both cases, and I'm not using any onclick functions, or any other sorts of events in the form.

推荐答案

检查你没有两次声明你的控制器:一个在HTML中,如上所示,另一个在配置路由时。如果是这种情况,控制器会实例化两次,因此监听器被调用两次

Check you didn't declare your controller twice: one in the HTML, as I can see above, and another when configuring your routes. If it's the case, the controller is instanciated twice, so the listener is invoked twice

这篇关于Angular ng-submit调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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