在角度工厂内不能使用$ http(角度1.x)

时间:2018-02-07 15:22:24

标签: angularjs http angular-http

我在脚本开头添加了$ http但由于某种原因没有加载$ http - 如何将$ http记录到模块而不是控制器

var abcdReportServices = angular.module('abcdReportServices', [ ]);

abcdReportServices.factory('uploadFileAjax', ['getPDFsImage', function(getPDFsImage) {
   return function(evt, $scope){
           $scope.http({
              method: "POST",
              url: 'someEndpoint/doSomething',
               data: $.param({
                 'name': 'name-of-rpt'
            }),
        headers: {
            "Content-Type" : "application/x-www-form-urlencoded"
        }
    }).success(
        function(data) {
            console.log("Saving success", data); 
        }
    );
   }

}
}]);

//控制台日志中的错误

$http is not defined...

1 个答案:

答案 0 :(得分:3)

您只需在工厂的依赖注入中加入$http,然后将$scope.http切换为$http

var abcdReportServices = angular.module('abcdReportServices', [ ]);

abcdReportServices.factory('uploadFileAjax', ['getPDFsImage', '$http', function(getPDFsImage, $http) {
   return function(evt){
           $http({
              method: "POST",
              url: 'someEndpoint/doSomething',
               data: $.param({
                 'name': 'name-of-rpt'
            }),
        headers: {
            "Content-Type" : "application/x-www-form-urlencoded"
        }
    }).success(
        function(data) {
            console.log("Saving success", data); 
        }
    );
   }

}
}]);