$q返回承诺两次$http服务调用

时间:2022-10-07 08:25:56

I'm very new angular js, Here trying to create service for default loading data. The current function I have loops over each api URL and pushes each into an array and then pushes. I had this functioning, returning array, which needed to be flattened out. Why is it being logged twice?

我是一个全新的角度js,这里尝试为默认加载数据创建服务。当前函数在每个api URL上循环,将每个URL推入一个数组,然后推入。我有这个功能,返回数组,它需要被平铺。为什么它被记录两次?

cat.service('defaultload', [ '$http', '$q', '$rootScope',function($http, $q, $rootScope){
    this.getdefaultload = function(){
        var deferred = $q.defer();
        var defaulturl = ["/cat_questionWithAnswers", "/cat_user_template", "/cat_evaluation", "/cat_newForm", "/cat_sub_key_template_1"];
        var collectdefaultdata = [];
        angular.forEach(defaulturl, function(url, index){
            console.log( $rootScope.serviceURL + url + " ---- "+ index);
            collectdefaultdata.push($http.get($rootScope.serviceURL+url));
        });
        $q.all(collectdefaultdata).then(function(results){
        deferred.resolve(JSON.stringify(results));
        console.log(JSON.stringify(results, null, 2));
    });
    return deferred.promise;
    }
}]);

cat.controller('Loginctrl', ['$scope', '$rootScope','$state','$log','$http','defaultload', function($scope, $rootScope, $state, $log, $http, defaultload){

    $rootScope.serviceURL = 'https://dummy.com';
    defaultload.getdefaultload();

}]);

2 个解决方案

#1


1  

You are doubling up your use of promises. $http is already a promise, and thus so is $q.all

你会加倍使用承诺。$http已经是一个承诺,因此$q.all也是

cat.service('defaultload', [ '$http', '$q', '$rootScope',function($http, $q, $rootScope){
    this.getdefaultload = function(){
        var defaulturl = ["/cat_questionWithAnswers", "/cat_user_template", "/cat_evaluation", "/cat_newForm", "/cat_sub_key_template_1"];
        var collectdefaultdata = [];
        angular.forEach(defaulturl, function(url, index){
            console.log( $rootScope.serviceURL + url + " ---- "+ index);
            collectdefaultdata.push($http.get($rootScope.serviceURL+url));
        });
        return $q.all(collectdefaultdata);
    }
}]);

And then you can use that in your controller

然后你可以在控制器中使用它

cat.controller('Loginctrl', ['$scope', '$rootScope','$state','$log','$http','defaultload', function($scope, $rootScope, $state, $log, $http, defaultload){

    $rootScope.serviceURL = 'https://dummy.com';
    defaultload.getdefaultload()
    .then(function(results) {...}));

}]);

#2


1  

Simple service example

简单的服务示例

.service("functionName",function($http,$q) {
   var abcObject = {}; //name of object you wanted to return
   var abc; //you can use $scope.abc
   abcObject.getValues = function () {
      var d = $q.defer(); //A new instance of deferred is constructed by calling $q.defer().
      $http.get('api.json').then(
         function success(response) {
            abc = response.data; // store the response into the abc
            d.resolve(abc); //return success
         },
         function failure(reason) {
            d.reject(reason);//return failure
         });
   return d.promise;//return result
   return abcObject;//return object of success or failure
});

Controller

控制器

.controller('CtrName', function(functionName){
   $scope.data = functionName.getValues();
});

#1


1  

You are doubling up your use of promises. $http is already a promise, and thus so is $q.all

你会加倍使用承诺。$http已经是一个承诺,因此$q.all也是

cat.service('defaultload', [ '$http', '$q', '$rootScope',function($http, $q, $rootScope){
    this.getdefaultload = function(){
        var defaulturl = ["/cat_questionWithAnswers", "/cat_user_template", "/cat_evaluation", "/cat_newForm", "/cat_sub_key_template_1"];
        var collectdefaultdata = [];
        angular.forEach(defaulturl, function(url, index){
            console.log( $rootScope.serviceURL + url + " ---- "+ index);
            collectdefaultdata.push($http.get($rootScope.serviceURL+url));
        });
        return $q.all(collectdefaultdata);
    }
}]);

And then you can use that in your controller

然后你可以在控制器中使用它

cat.controller('Loginctrl', ['$scope', '$rootScope','$state','$log','$http','defaultload', function($scope, $rootScope, $state, $log, $http, defaultload){

    $rootScope.serviceURL = 'https://dummy.com';
    defaultload.getdefaultload()
    .then(function(results) {...}));

}]);

#2


1  

Simple service example

简单的服务示例

.service("functionName",function($http,$q) {
   var abcObject = {}; //name of object you wanted to return
   var abc; //you can use $scope.abc
   abcObject.getValues = function () {
      var d = $q.defer(); //A new instance of deferred is constructed by calling $q.defer().
      $http.get('api.json').then(
         function success(response) {
            abc = response.data; // store the response into the abc
            d.resolve(abc); //return success
         },
         function failure(reason) {
            d.reject(reason);//return failure
         });
   return d.promise;//return result
   return abcObject;//return object of success or failure
});

Controller

控制器

.controller('CtrName', function(functionName){
   $scope.data = functionName.getValues();
});