防止AngularJS中的get请求中出现重复的代码

时间:2014-12-14 17:39:01

标签: angularjs timeout watch

我在过滤器按钮上有一个手表。当用户点击它时,呼叫观察者。 我需要将get请求发送到服务器。获取响应,保存并立即询问下一个获取请求。一个接一个。

$scope.$watch('dataService.getValue()', function (newValue, oldValue) {
        if (newValue !== oldValue) {

            var selectedValue = $scope.dataFilterService.getValue();
            $scope.httpService.getFilteredData(selectedValue).then(function (response) {
                var filteredData = response.data;
                $scope.httpService.getFilteredData2(selectedValue ).then(function (response) {
                    var filteredData2 = response.data;
                    $scope.httpService.getData3().then(function (response) {
                        var filteredData3 = response.data;
                        $scope.httpService.getData4().then(function (response) {
                            var filteredData4= response.data;
                            $scope.httpService.getData5().then(function (response) {
                                var filteredData5= response.data;

                                $scope.dataToMapService.init(filteredData, filteredData2 , filteredData3, filteredData4, filteredData5);


                            }, function (error) {
                                $scope.errorService.handleError(error);
                            });


                        }, function (error) {
                            that.errorService.handleError(error);
                        });
                    }, function (error) {
                        that.errorService.handleError(error);
                    });
                }, function (error) {
                    that.errorService.handleError(error);
                });

            }, function (error) {
                that.errorService.handleError(error);
            });

        }
    });

此代码工作正常,但它在两个控制器中重复。

为了防止重复代码我建议添加新服务并在其中添加代码。但是,它并没有像我预期的那样做。它没有等到第一个请求的答案。 我该怎么做才能更好地编写它并防止重复的代码?

1 个答案:

答案 0 :(得分:1)

使用$ q和$ http calls数组来做出一个承诺:

angular.module('myApp').factory('getDataService', function ($q, $http, httpService) {

    var service = {
        getData: function () {
            var defer = $q.defer();
            var callChain = [];
            callChain.push(httpService.getFilteredData(selectedValue));
            callChain.push(httpService.getFilteredData2(selectedValue));
            callChain.push(httpService.getData());
            callChain.push(httpService.getData2());
            callChain.push(httpService.getData3());
            $q.all(callChain).then(
                function (results) {
                    console.log(results); //Array of results in the order of the send
                    defer.resolve(results);
                });
            return $q.promise;

        }
    }
    return service;
}).controller('myCtrl', ['$scope', 'getDataService', function($scope, getDataService) {

    getDataService.getData().then(function(result) {
        console.log(results); //Array of results in the order of the send
    });
}]);