我做一个简单的角度应用来管理收入即可。来自我在JSON存储(用于测试目的)项目的收入。
I'm making a simple Angular app to manage incomes. The incomes come from projects that I store in json (for testing purpose).
所以,基本上,我用角服务来得到这个JSON和我想有调用服务项目其他服务和过滤各个项目的收入。
So, basically, I use a service in Angular to get this json and I would like to have another service that call Projects service and filter the incomes of each projects.
这里的code:
facturateApp.service('Projects', function($http){
var Projects = {
async: function() {
var promise = $http.get('base.json').then(function (response) {
return response.data;
});
return promise;
}
};
return Projects;
});
facturateApp.service('Incomes', function(Projects){
var incomes = [];
Projects.async().then(function(d) {
var projects = d;
angular.forEach(projects, function(project){
if(typeof(project.account.accountAmount) == 'number' && project.account.accountAmount > 0){
var newIncome = {};
newIncome.projectName = project.projectName;
newIncome.clientName = project.clientName;
newIncome.typeIncome = "Accompte";
newIncome.amount = project.account.amountAccount;
newIncome.date = project.account.accountDate;
newIncome.notes = project.account.accountType;
incomes.push(newIncome);
}
});
angular.forEach(projects, function(project){
if (typeof(project.total.totalAmount) == 'number' && project.total.totalAmount > 0){
var newIncome = {};
newIncome.projectName = project.projectName;
newIncome.clientName = project.clientName;
newIncome.typeIncome = "Accompte";
newIncome.amount = project.total.totalAmount;
newIncome.date = project.total.totalDate;
newIncome.notes = project.total.totalType;
incomes.push(newIncome);
}
});
});
console.log(incomes); // still returns [];
return incomes;
});
我的问题是,最后一个日志返回一个空数组。其实它的工作原理像预想的那样:收入
之前返回Projects.async(),然后(函数(D)
是执行。
My problem is that the last log returns an empty array. Actually it works as supposed : incomes
is returned before Projects.async().then(function(d)
is executed.
但我不知道等待这个函数调用其他服务的服务返回收入
之前执行...
But I don't know wait for this function to be executed before returning incomes
in a service that call another service...
任何帮助吗?
推荐答案
这是异步调用如何工作的:如果收入
业务数据通过异步的方式填充,因此必须返回承诺为好。
This is how async call works: If Incomes
service data populates by async way, therefore it must return promise as well.
我想创造一些方法 buildAndGetIncomes
将调用 Projects.async()
当数据获取 - >返回新承诺的控制器:
I would create some method buildAndGetIncomes
that will call Projects.async()
and when data is fetched -> return new promise to controller:
facturateApp.service('Incomes', function(Projects, $q){
var self = this;
var deferred = $q.defer();
var incomes = [];
self.buildAndGetIncomes = function(){
Projects.async().then(function(d) {
var projects = d;
angular.forEach(projects, function(project){
if(typeof(project.account.accountAmount) == 'number' && project.account.accountAmount > 0){
var newIncome = {};
newIncome.projectName = project.projectName;
newIncome.clientName = project.clientName;
newIncome.typeIncome = "Accompte";
newIncome.amount = project.account.amountAccount;
newIncome.date = project.account.accountDate;
newIncome.notes = project.account.accountType;
incomes.push(newIncome);
}
});
angular.forEach(projects, function(project){
if (typeof(project.total.totalAmount) == 'number' && project.total.totalAmount > 0){
var newIncome = {};
newIncome.projectName = project.projectName;
newIncome.clientName = project.clientName;
newIncome.typeIncome = "Accompte";
newIncome.amount = project.total.totalAmount;
newIncome.date = project.total.totalDate;
newIncome.notes = project.total.totalType;
incomes.push(newIncome);
}
});
deferred.resolve(incomes);
});
return deferred.promise;
};
});
和从控制器,我们称之为:
and from controller we call :
Incomes.buildAndGetIncomes().then(function(incomes) {
// ...
}, function(error) {
// ..
});
相关推荐
最新文章