Submit form with ng-submit and trigger synchronous post request

Problem

I have a form that I want to trigger validation on when the user clicks submit. If the validation fails, then suitable error messages are displayed. This much works.

However if the validation passes I want the form to submit a synchronous POST request with full page reload as if the action and method parameters were set as usual.

How does one achieve trigger the normal post action (not AJAX) from the ng-submit function on the AngularJS scope?

My form of course looks basically like the following:

<form id="myForm" name="myForm" ng-submit="formAction(this, models)">
  ...
 <input type="submit" value="Submit">
</form>

The best I can think of is to mirror the contents of the form with another hidden form submitting that one, but there must be a better way!

TO CLARIFY: If validation passes, I need the form submission to essentially behave like a normal synchronous post form submission which lands the user at the page returned by the server from the post request.

Problem courtesy of: Nat

Solution

http://plnkr.co/edit/cgWaiQH8pjAT2IRObNJy?p=preview

Please check this plunkr

Basically what I am doing is passing the $event object. form is the target of the event object, and we can submit it.

function Ctrl($scope) {
    $scope.list = [];
    $scope.text = 'hello';
    $scope.submit = function($event) {
      if ($scope.text) {
        $scope.list.push(this.text);
        if(this.text === 'valid'){
          $event.target.submit();
        }
        $scope.text = '';

      }
    };
  }
Solution courtesy of: jintoppy

Discussion

Try inside formAction after you've submitted the data:

$route.reload();
Discussion courtesy of: Wawy

I dont think you need to do a full page refresh. You have a single page app I am assuming; use it. Try something like this:

<section class="contact">
<article>
    <h1>Contact</h1>
    <form role="form" name="contactForm" ng-submit="formSubmit(contactForm)">
        <div class="form-group">
            <input class="form-control" ng-model="name" name="name" id="name" type="text" placeholder="Name" required/>
        </div>
        <div class="form-group">
            <input class="form-control" ng-model="email" name="email" id="email" type="email" placeholder="Email Address" required/>
        </div>
        <div class="form-group">
            <textarea class="form-control" ng-model="message" name="message" id="message" rows="5"></textarea>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary btn-lg">Send Message</button>
            <a class="btn btn-default btn-lg" href='mailto:me@something.net'>Or email me</a>
        </div>
    </form>
</article>

'use strict';

MyApp.controller('ContactController', function ContactController ($scope, EmailService) {
$scope.formSubmit = function(form) {

    EmailService.send(form).then(function(data) {

        if(data.message.sent) {
            $scope.resetForm();
            alert("Message Sent");
        }
        else {
            alert("Something went wrong. Try emailing me.");
        }
    });
}

$scope.resetForm = function() {
    $scope.name = "";
    $scope.email = "";
    $scope.message = "";
}
});

MyApp.factory('AjaxService', function AjaxService ($q, $http) {
return {
    http: function(ajaxParams) {
        var deferred = $q.defer();

        $http(ajaxParams)
            .success(function (data, status, headers, config) {
                deferred.resolve({
                    success: true,
                    status: status,
                    message: data
                });
            })
            .error(function (data, status, headers, config) {
                deferred.reject({
                    success: false,
                    status: status,
                    message: "Http Error"
                });
            });

        return deferred.promise;
    }
}
});

MyApp.factory('EmailService', function EmailService (AjaxService) {

return {
    send: function(emailData) {

        var ajaxParams = {
            method: 'POST',
            url: ''//where ever your form handler is,
            data: {
                name: emailData.name.$modelValue,
                email: emailData.email.$modelValue,
                message: emailData.message.$modelValue
            },
            cache: false
        }

        return AjaxService.http(ajaxParams);
    }
}
});
Discussion courtesy of: Jordan Papaleo

This recipe can be found in it's original form on Stack Over Flow.