2014-09-30 68 views
4

我正在製作一個簡單的Angular應用程序來管理收入。收入來自我存儲在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; 

}); 

我的問題是,最後的日誌返回一個空數組。其實它的工作原理如下:在執行Projects.async().then(function(d)之前返回incomes

但我不知道等待這個功能在返回調用另一個服務的服務incomes之前執行...

任何幫助嗎?

回答

3

這就是異步調用的工作方式:如果Incomes服務數據由異步方式填充,因此它也必須返回promise。

我會創造一些方法buildAndGetIncomes將調用Projects.async(),當數據被取出 - >返回新承諾控制器:

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

和控制器,我們稱之爲:

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

是否有可能有更簡單的東西在控制器中調用它(我的意思是像「收入」那樣返回'收入'數組)? – enguerranws 2014-09-30 09:51:33

+0

@enguerranws'Incomes.buildAndGetIncomes.'將返回收入數組。很簡單。其他方式,如果你想,在控制器端實現承諾鏈,但如你所願 – 2014-09-30 09:53:11

+0

好的,謝謝你的回答。你的代碼只是一個小錯誤:Incomes.buildAndGetIncomes()。then(function(d){} – enguerranws 2014-09-30 09:54:48