6

我已经构建了一个简单的服务,可以发出多个请求。该服务有两种方法。我无法从服务内部的另一种方法调用一种方法。

Plunkr:http ://plnkr.co/edit/2fERik4uTxbxlVOhncMd?p=preview

app.factory('Report', ['$http', function($http){
var Authors = {

    reports : [],
    requests :[{'url':'data/data.cfm','response':'first'},
               {'url':'data.json','response':'second'},
               {'url':'data.json','response':'third'},
               {'url':'data.json','response':'forth'}],


getReport : function(target, source, response, callback) {
    return $http({  url:source, 
                    method:"POST", 
                    params:{url : target}
                }).success(function(result) {
                    $scope.progress = response;
                    angular.extend($scope.user, result)
                    console.log($scope.user)
      }
      ).error(function(error){
                    $scope.progress = response
                })
},

    startQueue : function (target) {
        var promises = [];
        this.requests.forEach(function (obj, i) {
            console.log(obj.url)
            promises.push(getReport(target, obj.url, obj.response, function(value){
                reports.push(value);
                console.log(value)
            }));
        });
        $q.all(promises).then(function () {
            console.log("Finito");
        },function(error){
            console.log("errori")
        });
    }

};

return Authors;
}])

当我尝试从 startQueue 内部调用 getReport 时出现错误:未定义 getReport。

4

2 回答 2

13

将您的工厂更改为:

app.factory('Report', ['$http', function($http){
    var Authors = {

        reports : [],
        requests :[{'url':'data/data.cfm','response':'first'},
                   {'url':'data.json','response':'second'},
                   {'url':'data.json','response':'third'},
                   {'url':'data.json','response':'forth'}],
    };

    Authors.getReport = function(target, source, response, callback) {

    };
    Authors.startQueue = function (target) {

    };

    return Authors;
}])
于 2013-09-17T15:29:58.707 回答
1

我知道这“真的很晚”,但看起来是因为你没有注入 $q 。

改变:

app.factory('Report', ['$http', function($http){

app.factory('Report', ['$http','$q', function($http,$q){
于 2015-01-27T13:50:52.583 回答