繁体   English   中英

如何在angularjs中同时执行$ http

[英]How to execute simultaneously an $http in angularjs

我有一个模块(app),其中包含带有$http的service(globalService)内部。 该模块现在包含在另一个也具有服务(invoicesService)的模块(invoicesApp)中。 使用第二个模块的服务,我正在加载第一个服务的服务,并返回一些信息。 我遇到的问题是第一服务的参数已从第二服务覆盖,因此两个$http都在执行相同的请求。 这是我的代码

这是我的第一项服务:

(function(angular){
  var app = angular.module('app',[])
    .config(function($locationProvider) {
        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });
    })
    .service('globalService', function($http, $location,$q){
        console.log("globalService");

        this.getPaging = function()
            {
                var deferred = $q.defer();
                var paging_parameters = $location.search();
                paging_parameters.action = "client_pagination";
                var req = {
                    url: ajaxpagingscript.ajaxurl,
                    method: "GET",
                    params: paging_parameters
                }
                $http(req).success(function(paging_data) {
                    console.log("globalService " + JSON.stringify(paging_data));
                    deferred.resolve(paging_data.paging);
                }).error(function(data, status, headers)
                {
                    $scope.error = "";
                });

                return deferred.promise;
            }
        });
 })(window.angular);

第二服务:

(function(angular){

    var invoicesApp = angular.module('invoicesApp',['ngRoute','app'])
         .config(function($locationProvider,$routeProvider) {
            $locationProvider.html5Mode({
                enabled: true,
                requireBase: false
            });
         })
        .controller('InvoicesController', ['$scope','transactionService',
        function($scope,invoicesService) {
            $scope.invoices = [];
            console.log("testing");

            var paging = invoicesService.getPaging();
            var promise = invoicesService.getInvoicess();
            promise.then(function(data)
            {
                console.log("invoices " +JSON.stringify(data));
                angular.forEach(data.new_data, function(JSON_ARRAY) {
                    $scope.invoicearray.push(JSON_ARRAY);
                });
            });
        }])
        .service('invoicesService', function(globalService, $http,$location,$q){

            this.getPaging = function() { return globalService.getPaging(); };
            this.getInvoicess = function() {
                    var deferred = $q.defer();
                    var invoices_parameters = $location.search();
                    invoices_parameters.action = "collect_invoice_data";

                    var req = {
                        url: ajaxpagingscript.ajaxurl,
                        method: "GET",
                        params: invoices_parameters
                    };
                    $http(req).success(function(data) {
                        deferred.resolve(data);
                    }).error(function(data, status, headers)
                    {
                        $scope.error = "";
                    });

                    return deferred.promise;
            };

        });
})(window.angular);

这两个服务的console.log,返回相同的数据。 加载此应用程序的页面将加载invoicesApp因此可以很好地执行globalService,但是传递给$http的参数是错误的。 谁能帮我?

您不需要调用该函数,而如果要在invoicesService中尝试这样做,则需要传递该函数的引用。

this.getPaging = function() { return globalService.getPaging(); };

应该

this.getPaging = globalService.getPaging;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM