Angular ng-submit called twice
Asked Answered
P

6

15

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
    });
});

Console Window:

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…}

So, the first thing I tried was to stop propagating the event, but that doesn't 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".

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.

Parton answered 6/3, 2014 at 21:12 Comment(3)
how are you submitting this form, just clicking the button? And do you have any jQuery code attached to either the form or the button?Bootle
Submitting via button click, and there is no additional code.Parton
This plunkr works fine, there's something more to the story that's not posted in your question. plnkr.co/edit/qdj2tUPGWfpON2XFfvwG?p=previewFreckly
R
12

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

Rella answered 6/3, 2014 at 21:17 Comment(3)
Right now I'm not configuring any routes. This is just a single page at this time, so what you see up there is really all I have.Parton
Belay that, I was actually loading my App.js twice, so it was duplicating controllers.Parton
Just if anyone has this same issue. I was putting (ngSubmit)="submit(true)" on the form tag and also (click)="submit()" on the button. Thank you.Carding
N
13

Another reason for this occurring (which just happened to me):

I had the following:

<form ng-submit="myCtrl.search()">
   <button type="submit">Search</button>
   <button type="submit" class="mobile-only" ng-click="myCtrl.search()">Go</button>
</form>

I had another button inside the form that was bound to the same function as the ng-submit on its ng-click which was causing the method to be called twice.

Novotny answered 3/12, 2014 at 16:45 Comment(2)
in such scenario remove the ng-click, and still when you click the submit button, the function will be called because the button type is submitRigadoon
Wow had no Idea i was blaming the backend serviceBehm
R
12

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

Rella answered 6/3, 2014 at 21:17 Comment(3)
Right now I'm not configuring any routes. This is just a single page at this time, so what you see up there is really all I have.Parton
Belay that, I was actually loading my App.js twice, so it was duplicating controllers.Parton
Just if anyone has this same issue. I was putting (ngSubmit)="submit(true)" on the form tag and also (click)="submit()" on the button. Thank you.Carding
B
7

One reason happend to me, I'm not sure if this will happen to any other :)

In my controller I had:

$scope.submit = function() { someThingHere };

In the view

<form ng-submit="submit()">
</form>

This way that form has been submitted "mysteriously" two times, to solve that I had to rename $scope.submit to something else.

Bannon answered 18/12, 2014 at 23:15 Comment(0)
R
7

There are multiple causes for such behaivour (some of them noted already):

  • Declaring the controller twice (Check routes, html header, container html (if there is one).
  • Naming a function submit (Even if i could not reproduce it).
  • Set a ng-click function on the submit button when you have it already on the form ng-submit, just remove the ng-click. Clicking on the submit button or pressing enter on a input will call the ng-submit function by itself.

Double check them since it's easy to pass them by in some cases

Ramos answered 26/7, 2016 at 11:6 Comment(4)
The 3rd one was a solution for me. Thanks, slipped my eyes somehow.Centreboard
The 3rd one was a solution for me too!Enphytotic
I can reproduce 2 with @Output() submit: EventEmitter<string>Aldehyde
Event can be received twice also if it's called "search" : @Output() search: EventEmitter<string>. Then parent component can receive your emitted event and native search event developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/… Renaming the eventEmmiter helps in this case.Centigrade
S
1

I experienced this in Angular 12 with a reactive form because I had an unnecessary ngForm attribute on the form declaration:

  <form ngForm [formGroup]="formGroup" (ngSubmit)="onSubmit()">

Removing the attribute fixed the problem.

Scapolite answered 31/1, 2022 at 12:47 Comment(1)
This was the issue for me! Thank you <3Trotline
S
0

In my case, it was "ng-app" directive which I was not relying on as I manually bootstrap everything.

Susceptive answered 26/10, 2018 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.