Skip to content Skip to sidebar Skip to footer

Angularjs Get Form Action And Submit To It

I have a form and I want to catch it submission, check validation of data and than submit form to the action inside the HTML form.

Solution 1:

You should:

  1. Use the ng-submit directive on your form
  2. Pass the form element to your save() method
  3. Use the $http service to post

var ctrl = function ($scope, $http, $log) {
$scope.save = function (form) {
    //if (!$scope.contactForm.$valid) return;var url = form.attributes["target"];
    $log.debug(url);
  
    $http
      .post(url, { email: $scope.email, name: $scope.name })
      .success(function (response) {
        $log.debug(response);
      })
    }
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="app"ng-controller="ctrl"><formng-submit="save($element.action)"><buttontype="submit">Send Message</button></form></div>

Solution 2:

I really advice you to follow this page of the docs from the beginning to the end, you'll change your approach to form using AngularJS :)

https://docs.angularjs.org/guide/forms

Solution 3:

Use ng-submit instead on the form element

"Additionally it prevents the default action (which for form means sending the request to the server and reloading the current page), but only if the form does not contain action, data-action, or x-action attributes."

It will also only run the expressions if the native html5 validation is valid

Post a Comment for "Angularjs Get Form Action And Submit To It"