0
在Angular中,我有一項訪問Google聯繫人api的服務,然後將所有聯繫人存儲到變量(StorageVar
)中,以供服務中的其他功能分析。在已解決的Angular承諾之後調用其他服務功能
我應該在從API獲取數據之前調用其中一個分析函數,然後我想調用getAPI
函數(如.then()
)運行分析函數。問題是,我無法弄清楚如何訪問.then()
範圍內的任何功能。
angular.module('API',[])
.factory('API', ['$rootScope', '$http','$q', function ($rootScope, $http, $q) {
var StorageVar = [];
return{
getAPI: function(){
//does async call, returns promise, stores data into StorageVar
}
Analyze: function(){
if(StorageVar.length==0){
//need to get the data first
this.getAPI().then(function(){
//Analyze()
debugger;
}
}
}
在我的控制器我希望用我的服務是這樣的:
angular.module('views.local', ['API'])
.controller('localctrl',['API',function(API){
API.Analze()
//Callable even if the getAPI function hasn't run, so the Analyze function will take care of that.
}])
任何援助表示讚賞。
那麼你能展示一個關於如何使用API工廠的例子嗎? – Wawy
@Wawy,我編輯了這個問題來添加更多關於我正在使用的控制器的信息。你是這個意思嗎?如果需要更多的API調用,我也可以爲分析功能實現承諾結構,但我還沒有得到那麼多。 – tristansokol
你想在裏面訪問什麼函數。然後回調函數? – Wawy