search

Home  >  Q&A  >  body text

angular.js - Can $http in AngularJS submit the entire form together?

I changed a project in my company and changed $.ajax in JQuery to $http in AngularJS. When submitting data, it used to be $("#form").serialize(), but now to submit data, I need to List them all and encapsulate them into json for submission. I just want to ask if there is a way in angularjs to submit all the form data at once like the former. (PS: The company requires not to use JQuery in the project)

黄舟黄舟2866 days ago562

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-15 17:08:22

    <input ng-model="user.name">
    <input ng-model="user.age">

    Get the object by naming it ng-modelcontroller中直接使用$scope.user as above.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:08:22

    Specify the name attribute for the form element, and then the form will define a form controller with this name in the scope where it is located.

    For example

    <form name="myForm">
      <input name="myInput">
    </form>

    Then use the following operations to get the value

    function getFormValue(formCtrl) {
      return Object
        .keys(formCtrl)
        .filter(function(key) {
          return key[0] != '$';
        }
        .reduce(function(res, key) {
          res[key] = formCtrl[key].$modelValue;
          return res;
        }, {});
    }
    
    getFormValue($scope.myForm);
    // output: {myInput: "..."}

    reply
    0
  • Cancelreply