如何使用angular将表单数据发送到服务器?

时间:2014-01-01 03:32:59

标签: javascript angularjs cakephp

目前,我正在使用服务器端框架CakePHP来构建我的应用程序。

我需要将角度集成到我的应用中。

目前,我发送以下输入

User.old_passwordUser.new_passwordUser.new_password_confirm

使用POST

到此网址/mypasswords/new

这很好用。

据我所知,要将数据发送到服务器端,我需要使用Angular中的服务。

到目前为止,这是我的Angular代码。

var app = angular.module("myApp", []);

app.controller('passwordController', ['$scope', function($scope) {
  $scope.submitted = false;
  $scope.changePasswordForm = function() {
    if ($scope.change_password_form.$valid) {
      // Submit as normal
    } else {
      $scope.change_password_form.submitted = true;
    }
  }
}]);

services = angular.module('myApp.services', ['ngResource']);

services.factory("UserService", function($http, $q) {
  var service;
  // service code here
});

那么我在//service code here部分写什么?我错过了哪些其他部分?

我的表单在呈现后目前看起来像html。 (我知道我可能要从表单中删除动作和方法属性。是吗?)

<form action="/mypasswords/new" id="change_password_form" name="change_password_form" ng-controller="passwordController" ng-submit="changePasswordForm()" method="post" accept-charset="utf-8" class="ng-scope ng-pristine ng-valid">

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>            

<input name="data[User][old_password]" id="u56" placeholder="Enter old password..." class="u56 profile_input" type="password">
<input name="data[User][new_password]" id="u57" placeholder="Enter new password..." class="u57 profile_input" type="password">
<input name="data[User][new_password_confirm]" id="u58" placeholder="Enter new password..." class="u58 profile_input" type="password">
<div id="u25" class="u25_container">
    <div id="u25_img" class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
     </div>
</div>

<div id="u27" class="u27_container">
    <input id="u27_img" class="submit_button detectCanvas" type="submit" value="SAVE">                         

</div>
</form>

谢谢。

2 个答案:

答案 0 :(得分:1)

第1步:使用普通的html而不是CakePHP中的FormHelper来编写表单

表单应如下所示:

<form name="change_password_form" ng-controller="passwordController"
         ng-submit="changePasswordForm()">

        <input name="data[User][old_password]" ng-model="data.User.old_password" type="password" placeholder="Enter old password..." class="u56 profile_input">
        <input name="data[User][new_password]" ng-model="data.User.new_password" type="password" placeholder="Enter new password..." class="u57 profile_input">
        <input name="data[User][new_password_confirm]" ng-model="data.User.new_password_confirm" type="password" placeholder="Enter new password again..." class="u58 profile_input">

            <div id="u25"
                 class="u25_container">
                <div id="u25_img"
                     class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
                </div>
            </div>

            <div id="u27"
                 class="u27_container">
                <button type="submit" id="u27_img" ng-click="debug()"
                     class="submit_button detectCanvas" />
            </div>
        </form>

第2步:按以下方式编写app.js.

如果你想使用CakePHP $this->request->is('ajax')

,那么X-Requested-With标头很重要
angular.module("myApp", [])
.controller('passwordController', function($scope, $http) {
  $scope.changePasswordForm = function() {
    console.log('change passwrd form activated');
    //note: use full url, not partial....
    $http({
        method  : 'POST',
        url     : '/mypasswords/new',
        data    : $.param($scope.data),  // pass in data as strings
        headers : { 'Content-Type': 'application/x-www-form-urlencoded',
        'X-Requested-With' : 'XMLHttpRequest'
        }  // set the headers so angular passing info as form data (not request payload)
    })
    .success(function(data, status, headers, config) {
        //do anything when it success..
        console.log('works!');
      })
    .error(function(data, status, headers, config){
        //do anything when errors...
        console.log('errors!');
    });
  }
  $scope.debug = function () {
   console.log($scope);
  }
});

答案 1 :(得分:0)

如果您只想发送带有ajax的表单,无需创建工厂,只需在控制器中执行此操作:

var app = angular.module("myApp", [$http]);

app.controller('passwordController', ['$scope', function($scope) {
  $scope.submitted = false;
  $scope.changePasswordForm = function() {
    if ($scope.change_password_form.$valid) {
      //note: use full url, not partial....
      $http.post('http://myweb.com/mypasswords/new', change_password_form.Data) 
      .success(function(data, status, headers, config) {
        //do anything when it success..
      })
      .error(function(data, status, headers, config){
        //do anything when errors...
      });
    } else {
      $scope.change_password_form.submitted = true;
    }
  }
}]);

您的表单将如下所示:

<form id="change_password_form" name="change_password_form" ng-controller="passwordController" ng-submit="changePasswordForm()" method="post" accept-charset="utf-8" class="ng-scope ng-pristine ng-valid">

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>            

<input name="data[User][old_password]" ng-model="Data.old_password" id="u56" placeholder="Enter old password..." class="u56 profile_input" type="password">
<input name="data[User][new_password]" ng-model="Data.new_password" id="u57" placeholder="Enter new password..." class="u57 profile_input" type="password">
<input name="data[User][new_password_confirm]" ng-model="Data.new_password_confirm" id="u58" placeholder="Enter new password..." class="u58 profile_input" type="password">
<div id="u25" class="u25_container">
    <div id="u25_img" class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
     </div>
</div>

<div id="u27" class="u27_container">
    <input id="u27_img" class="submit_button detectCanvas" type="submit" value="SAVE">                         

</div>
</form>