简体   繁体   中英

Angular form submitting twice

so I am making a user registration form with angular js using ng-submit and ng-model. The problem is when the user submits the form its triggers twice.

I looked around for common causes and none of them fit the bill. I haven't declared my controller twice and my submit button doesn't have any ng-click events.

Here is the form code (likely something wrong here that I am missing)

<form class="form"  ng-submit="registerUser()">
    <div class="form-group row">
       <label for="user-fname">First Name</label>
       <div class="two-for-one">
          <input type="text" id="user-fname" name="user-fname" class="form-control" placeholder="first name" ng-model="userData.fname">
      </div>
      <label for="user-lname">Last Name</label>
      <div class="two-for-one">
        <input type="text" id="user-lname" name="user-lname" class="form-control" placeholder="last name" ng-model="userData.lname">
      </div>
    </div>
    <label for="email">Email</label>
    <input class="form-control" type="text" id="email" name="email" placeholder="Email" rel="popover" data-content="What’s your email address?" ng-model="userData.email">
     <label for="reg_password">Password</label>
     <input class="form-control" type="password" id="reg_password" name="reg_password" placeholder="Password" rel="popover" data-content="Please choose a password (minimum of 6 characters)" ng-model="userData.pw">
     <label for="reg_password_confirm">Confirm Password</label>
     <input class="form-control" type="password" id="reg_password_confirm" name="reg_password_confirm" placeholder="Confirm Password" rel="popover" data-content="Please confirm your password" ng-model="userData.cpw">
     <label for="mail">Want to be apart of our mailing list?</label>
     <input type="checkbox" id="mail" name="mail" value="1" ng-model="userData.mail">
     <button class="btn btn-success">Next</button>
 </form>

And although I don't think its the js here it is:

       $scope.userData = {
            acc_type: "user",
            mail:false,
        };
        $scope.registerUser = function() {
            loginRegsterService.registerUser($scope.userData).then(function(response){
                console.log(response);
            });
        };

And the last part is a service which uses http to post data. Its pretty standard

        this.registerUser = function(data) {
            var req = {
                method: 'POST',
                url: location.protocol + '//' + location.host + '/models/register.php',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: data
            }
            return $http(req).then(function(response) {
                return response.data;
            });
        };

Thanks for reading and your help!

Oh and I forgot to mention that all this is work fine except it submits twice :\\

And the html is valid except for the angular directives.

Maybe because you tag has no type attribute specified - and it's treated as submit intialy? See here also : https://stackoverflow.com/a/4667996/405623 ?

It can happen if you create a child component which has a form and emits submit / search event to the parent component with EventEmitter. Then parent component will receive both your event and a native one. You should rename eventEmmiter in the child component, eg:

// Child component
@Output() submitForm = new EventEmitter();
@Output() searchSomething = new EventEmitter();

instead of

@Output() submit = new EventEmitter();
@Output() search = new EventEmitter();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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