2015-02-09 67 views
0

我想了解在提交表單時,客戶端如何工作。我在那裏一半,但我得到一個錯誤,我不能前進。如何在Angular js中爲註冊表單提供服務

在我的控制,我有以下代碼:

$scope.SaveData = function (data) { 
     if ($scope.submitText == 'Save') { 
      $scope.submitted = true; 
      $scope.message = ''; 

      if ($scope.isFormValid) { 
       $scope.submitText = 'Please Wait...'; 
       $scope.User = data; 
       RegistrationService.SaveFormData(data).then(function (response) { 
        alert(response); 
        if (response == 'Success') { 
         //have to clear form here 
         ClearForm(); 
        } 
        $scope.submitText = "Save"; 
       }); 
      } 
      else { 
       $scope.message = 'Please fill required fields value'; 
      } 
     } 
    } 

然後在我的註冊服務,我有以下幾點:

.factory('RegistrationService', function ($http, $q, $location) { 
    //here $q is a angularjs service with help us to run asynchronous function and return result when processing done 
    var fac = {}; 
    fac.SaveFormData = function (data) { 
     var defer = $q.defer(); 
     $http({ 
      url: '/api/Users', 
      method: 'POST', 
      data: d, 
      headers: { 'content-type': 'application/json' } 
     }).success(function (d) { 
      // Success callback 
      defer.resolve(d); 
      $locationu.url('/'); 
     }).error(function (e) { 
      //Failed Callback 
      alert('Error!'); 
      defer.reject(e); 
     }); 
     return defer.promise; 
    } 
    return fac; 
}); 

是什麼data:d代表什麼?我應該如何定義才能處理表單? 我有範圍內的數據,但當我打電話SaveFormData我遇到問題data:d它說未定義,我不明白爲什麼是這樣嗎?代碼的那部分是做什麼的? 歡迎任何參考教程和解釋

回答

1

這是因爲d沒有定義,「data:d」是您設置JSON對象的位置在請求中傳遞給服務器,使用ng-model =「formDara .fieldname「然後將formData傳遞給您的saveFormData(formData)並將」data:d「更改爲」data:data「。

相關問題