F
F
faragly2014-11-13 21:05:20
Angular
faragly, 2014-11-13 21:05:20

How to pass a POST request with form data and adding a new request parameter in AngularJS?

Hello! There is a need to send a post request with a transition to the page (with a reload, that is, without AJAX), but with the preliminary addition of a new parameter in the request. This function was implemented in jquery with the following code:

function RestoreDefaults()
{ 
   var form= $("form[name=my_form]"),
         restore = $(":submit[name=RestoreDefaults]", form);
   $("<input />", {type: "hidden", value: restore.val(), name: "RestoreDefaults"}).insertAfter(restore);
   form.submit();
}

In connection with the transition to angularjs, you need to get rid of jquery and rewrite this function accordingly. I am just starting to learn AngularJS, so this question seems to me elementary, but it is not yet within my power. Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rodion Meshcheryakov, 2014-11-13
@rodionme

The answer could be more precise if the meaning of the manipulations was clear, but for now something like this:
HTML:

<form name="loginForm" ng-submit="submitForm(loginForm)">
  <input type="email" ng-model="user.email">
  <input type="password" ng-model="user.password">
        
  <button type="submit">Войти</button>
</form>

JS:
.controller('LoginCtrl', function ($scope, $http) {
  $scope.user = {};

  $scope.user.customField = 'customValue';

  $scope.submitForm = function (form) {
      $http.post('path/to/your/url', $scope.user)
        .success(function (data) {
          console.log('data:', data);
        })
        .error(function (data) {
          console.log('Error:', data);
        });
  };
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question