我已经构建了一个提出多个请求的简单服务。该服务有两种方法。我无法在服务中调用另一个方法。
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。
答案 0 :(得分: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;
}])
答案 1 :(得分:1)
我知道这是'真的很晚',但看起来是因为你没有注入$ q。
变化:
app.factory('Report', ['$http', function($http){
到
app.factory('Report', ['$http','$q', function($http,$q){