有没有办法可以将$ http中的参数传递给对象的一部分?

时间:2014-03-12 17:17:35

标签: angularjs

我正在使用以下电话:

     $scope.retrieve = function () {

         $resource('/api/Test/Retrieve')
             .query({
                 subjectId: $scope.config.subjectId,
                 examId: $scope.config.examId,
                 userId: $scope.config.createdById
             },
             function (result) {
                 $scope.grid.data = angular.copy(result);
             },
          function () {
              $scope.grid.data = null;
          });
     };

有没有办法可以通过像这样的对象传递参数并使用$ http调用而不是$ resource。另外,我如何将成功和错误代码块移动到自己的函数?

1 个答案:

答案 0 :(得分:3)

以下代码适用于发布数据。

$http.post("/api/Test/Retrieve", {
               subjectId:$scope.config.subjectId,
               examId:$scope.config.examId,
               userId:$scope.config.createdById
             }).success(
             function(res){
               //Some success handler here
             }).error(
             function(res){
               //Some error handler here
             });

您可能想要包含许多细节,如果您需要带参数的GET,请查看配置参数及其属性:

http://docs.angularjs.org/api/ng/service/$http

//Same sample with handler functions moved out for ease of reading etc.

var successFunction = function(res){
}
var errorFunction = function(res) {
}
var params = {
               subjectId:$scope.config.subjectId,
               examId:$scope.config.examId,
               userId:$scope.config.createdById
             };

$http.post("/api/Test/Retrieve", params).
  success(successFunction).
  error(errorFunction);
相关问题