为什么承诺在循环之外不起作用?

时间:2015-06-02 15:45:41

标签: javascript angularjs

我正在尝试将fusionCharts动态添加到AngularJS页面中。我正在将图表数据加载到模型对象中,图表对象会显示,但“无数据显示”内容。

我的app.js:

(function () {

var modelListOfCharts = ["1", "4", "5", "6"];
var model = {};
var app = angular.module('chartDash', ["ng-fusioncharts"]);

app.controller('fussionController',["$scope","DataService", function ($scope, DataService) {
    $scope.chartData = {};

    DataService.getThings().then(function () {
        $scope.myListOfCharts = modelListOfCharts;
        $scope.chartData = model;
    });
}]);

app.service("DataService", ["$http", "$timeout", "$q", function ($http, $timeout, $q) {
    return {
        getThings: function () {
            var dfd = $q.defer();
            $timeout(function () {
                angular.forEach(modelListOfCharts, function(chartId) {
                    $http.get('/FusionCharts/GetChartData/', { params: { chartID: chartId } }).success(function (result) {
                        model[chartId] = result;
                    });
                });
                dfd.resolve(model);
            }, 300);
            return dfd.promise;
        }
    };
}]);
})();

我的HTML:

<div ng-controller="fussionController">
    <div ng-repeat="n in myListOfCharts">
        <h2>Chart number {{n}}</h2>
        <fusioncharts width="100%"
                      height="400"
                      type="MSCombi2D"
                      datasource="{{chartData[n]}}"></fusioncharts>
    </div>
</div>

当我把dfd.resolve(模型);在循环内部它可以工作,但只显示一个图表的数据。

    app.service("DataService", ["$http", "$timeout", "$q", function ($http, $timeout, $q) {
    return {
        getThings: function () {
            var dfd = $q.defer();
            $timeout(function () {
                angular.forEach(modelListOfCharts, function(chartId) {
                    $http.get('/FusionCharts/GetChartData/', { params: { chartID: chartId } }).success(function (result) {
                        model[chartId] = result;
                        dfd.resolve(model);
                    });
                });

            }, 300);
            return dfd.promise;
        }
    };
}]);

2 个答案:

答案 0 :(得分:2)

由于有多个http获取,因此会有多个promise。所以你必须等待所有这些承诺得到解决。试试这个。

app.service("DataService", ["$http", "$timeout", "$q", function ($http, $timeout, $q) {
    return {
        getThings: function () {
            var arrayOfPromises = [];
                angular.forEach(modelListOfCharts, function(chartId) {
                    arrayOfPromises.push($http.get('/FusionCharts/GetChartData/', { params: { chartID: chartId } }).success(function (result) {
                        model[chartId] = result;
                    }));
                 });

            return $q.all(arrayOfPromises);
        }
    };
}]);

答案 1 :(得分:0)

您需要为每个图表创建一个承诺,然后等待所有图表解决:

app.service("DataService", ["$http", "$timeout", "$q",
  function($http, $timeout, $q) {
    return {
      getThings: function() {
        var promises;

        $timeout(function() {
          promises = modelListOfChart.map(function(chartId) {
            var dfd = $q.defer();
            $http.get('/FusionCharts/GetChartData/', {
              params: {
                chartID: chartId
              }
            }).success(function(result) {
              model[chartId] = result;
              dfd.resolve(model);
            });
            // Might want to add a reject for errors...

            return dfd;
          });
        }, 300);

        return $q.all(promises);
      }
    };
  }
]);