AngularJS多个GET请求,只能首先正确返回

时间:2015-10-06 12:46:23

标签: angularjs promise ionic angular-promise

我的控制器中有以下内容:

ApiRequest.get('locations').then(function(locations) {
    $scope.locations = locations.locations;
});

ApiRequest.get('sublocations').then(function(sublocations) {
    $scope.sublocations = sublocations.sublocations;
});

ApiRequest.get('varieties').then(function (varieties) {
    $scope.varieties = varieties.varieties;
});

ApiRequest.get('tasks').then(function(tasks) {
    $scope.tasks = tasks.tasks;
});

ApiRequest.get('customers').then(function(customers) {
    $scope.customers = customers.customers;
});

ApiRequest.get('batches').then(function(batches) {
    $scope.batches = batches.batches;
    $ionicLoading.hide();
});

来自每个请求的数据继续弹出表单中的选择框。

这是我的APIRequest服务:

return {

        get: function(entity) {
            if($rootScope.online == false) {
                var data = {};
                data = JSON.parse(localStorage.getItem('data-' + entity));
                console.log(data);
                deferred.resolve(data);
            } else {
                $http.get($rootScope.baseUrl + entity).success(function(data) {
                    deferred.resolve(data);
                })
            }

            return deferred.promise;

        },
}

由于某种原因,结果似乎没有按时从服务中返回,以便在视图中显示它们。

这与我处理承诺的方式有关吗?

2 个答案:

答案 0 :(得分:3)

初看起来,你在你的函数之外用$ q声明了promise作为全局(因为我没有在里面看到)。试试这个:

get: function(entity) {
        var deferred = $q.defer();
        if($rootScope.online == false) {
            var data = {};
            data = JSON.parse(localStorage.getItem('data-' + entity));
            console.log(data);
            deferred.resolve(data);
        } else {
            $http.get($rootScope.baseUrl + entity).success(function(data) {
                deferred.resolve(data);
            })
        }

        return deferred.promise;

    },

答案 1 :(得分:1)

您当前的实现几乎没有错误处理,并且并行执行多个API请求;我建议链接承诺。

ApiRequest.get('locations').then(function(locations) {
    $scope.locations = locations.locations;

    return ApiRequest.get('sublocations');
}).then(function(sublocations) {
    $scope.sublocations = sublocations.sublocations;

    return ApiRequest.get('varieties')
}).then(function (varieties) {
    $scope.varieties = varieties.varieties;

    return ApiRequest.get('tasks')
}).then(function(tasks) {
    $scope.tasks = tasks.tasks;

    return ApiRequest.get('customers')
}).then(function(customers) {
    $scope.customers = customers.customers;

    return ApiRequest.get('batches')
}).then(function(batches) {
    $scope.batches = batches.batches;

    $ionicLoading.hide();
}, function(_error) {
    $ionicLoading.hide();
    console.log(_error);
});

然后您的服务可以简化; $http客户端返回一个承诺,使用$q.when也可以返回承诺

    get: function(entity) {
        if($rootScope.online == false) {
            var data = {};
            data = JSON.parse(localStorage.getItem('data-' + entity));
            console.log(data);
            $q.when(data);
        } else {
            return $http.get($rootScope.baseUrl + entity)
        }
    },