AngularJS 多个 GET 请求,只有第一次正确返回

AngularJS Multiple GET requests, only first returning correctly

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

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

来自这些请求中的每一个的数据继续填充表单中的 select 个框。

这是我的 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;

        },
}

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

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

乍一看,您在函数外部用 $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;

    },

您当前的实现几乎没有错误处理,并且正在并行执行多个 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 客户 return 是一个承诺,使用 $q.when 也可以 return 一个承诺

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