3

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.

21
  • Can you try giving type=submit for the button? Commented Apr 8, 2015 at 17:57
  • @maddog I just did and it still submits twice. Commented Apr 8, 2015 at 17:59
  • Submit twise you mean you enter registerUser function twise or it redirects the page also? Commented Apr 8, 2015 at 18:00
  • 1
    Well you code is fine, so the issue is not reproducible with the code you posted. I suggest that you add more code. Or make a demo. Commented Apr 8, 2015 at 18:03
  • 1
    plnkr.co/edit/lIhkfikpG8mR6lF10MaB?p=preview Commented Apr 8, 2015 at 18:04

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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 ?

Comments

0

check if your API is in the same domain as your application.

If the api ist on a webServer and your application is on localhost the form submitted twice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.