gpt4 book ai didi

javascript - Angular 服务调用另一个服务

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:48 30 4
gpt4 key购买 nike

我正在制作一个简单的 Angular 应用来管理收入。收入来 self 存储在 json 中的项目(用于测试目的)。

所以,基本上,我在 Angular 中使用一个服务来获取这个 json,我希望有另一个服务调用 Projects 服务并过滤每个项目的收入。

代码如下:

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;

});

我的问题是最后一条日志返回一个空数组。实际上它按预期工作:incomesProjects.async().then(function(d) 执行之前返回。

但我不知道在调用另一个服务的服务中返回 incomes 之前等待这个函数执行...

有什么帮助吗?

最佳答案

这就是异步调用的工作原理:如果 Incomes 服务数据以异步方式填充,因此它也必须返回 promise。

我会创建一些方法 buildAndGetIncomes ,它将调用 Projects.async() 并在获取数据时 -> 向 Controller 返回新的 promise :

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;
};
});

从 Controller 我们调用:

Incomes.buildAndGetIncomes().then(function(incomes) {
// ...
}, function(error) {
// ..
});

关于javascript - Angular 服务调用另一个服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26117441/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com