如何阻止最后一次AJAX请求响应调用覆盖数据?

时间:2016-09-10 04:47:27

标签: angularjs

我有多个图表,根据最后的API请求,图表只显示最后一个AJAX响应。但是在console.log中,console.log($scope.polls[index].data);显示正确的数据。但是使用$scope.data = [data];仅显示上一次AJAX请求所产生的数据。我该如何防止这种情况?我很长时间都无法做到这一点。

   adminApp.controller('PollController', function($scope, $window, $timeout, $rootScope, $routeParams, $http, $location, $q) {

        displayLoader("Loading...", "100px");
        var httpData1 = $http({
            method: 'get',
            url: "https://comments.wittyfeed.com/get_all_polls",
        }).
        success(function(response) {
            $scope.poll = response;
            for (var i = 0; i < response.length; i++) {
                getPollById(response[i], i);

                angular.element('#loadering').fadeOut('slow');
            }
        }).
        error(function(response) {
            // console.log(response || "Request failed");
        });

        function getPollById(i, index) {
            $http({
                method: 'post',
                url: BASE_URL + 'admin_api/admin_poll_api/admin_poll',
                data: i

            }).

            success(function(response) {
                appendData(response, index);    
            }).

            error(function(response) {
                // console.log(response || "Request failed");
            });
        }

        $scope.polls = [];

        function appendData(response, index) {
            $scope.polls[index] = response;

            var labels = [];
            var data = [];
            $scope.polls[index].data = data;
            $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];

            console.log($scope.polls[index].data);
            for (var i = 0; i < response.response.options.length; i++) {
                //  labels.push(response.response.options[i].options);
                data.push(response.response.options[i].votes);

            }
            $scope.data = [data];
            console.log(data);


        }


    });

我的观点

 <div>
 <canvas id="base" class="chart chart-bar"  chart-data="data" chart-labels="labels" chart-series="series" ></canvas>      
 </div>

0 个答案:

没有答案