在 Angular 中的已解决承诺后调用其他服务函数

Calling additional service functions after a resolved promise in Angular

在 Angular 中,我有一个服务可以访问 google 联系人 api,然后将我所有的联系人存储到变量 (StorageVar) 中以供其他函数分析在服务中。

我是否应该在从 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.
}])

如有任何帮助,我们将不胜感激。

你是这个意思吗?

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
                     var _this = this;
                     this.getAPI().then(function(){
                         //Analyze()
                         _this.Analyze();
                         debugger;
                     }
                }
            }