AngularJs promise in forEach

别等时光非礼了梦想. 提交于 2020-01-03 19:17:06

问题


I have method for getting data from server. And I use it in foreach, and after it need bind him to $scope variable. Like this:

var qualityMix = [];
var engagementMix = [];
angular.forEach(versions, function (versions) {
    qualityMix.push(qualityScoreByTimeWithAppVerPromise(versions.version));
    engagementMix.push(engagementByTimeWithAppVerPromise(versions.version));
});

$scope.qualityScoreByTimeMix = function () {
    return $timeout(function () {
        return $q.all(qualityMix).then(function (data) {
            return {series: data};
        });
    });
};

$scope.engagementTimeMix = function () {
    return $q.all(engagementMix).then(function (data) {
        return {series: data};
    });
};

qualityScoreByTimeWithAppVerPromise and engagementByTimeWithAppVerPromise it is functions for getting data from server. Then $scope.engagementTimeMix and $scope.qualityScoreByTimeMix need return functions with promise (is okay).

This code working but not always, some times I catch exceptions $scope.xxx is not a function.

I don't know how to fix it. Help me please. Thanks a lot!

UPD It is code for build charts.

<div class="section">
    <highchart id="mix_quality_score_by_time" type="area" data="qualityScoreByTimeMix"
               chart-style="qualityScoreMixChartStyle"></highchart>
</div>

And my directive I invoke in other page, like this:

<compare-versions id="compare_versions_panel" start-day="timeFilter.startDay()"></compare-versions>

$scope.xxx is not a function I mean what I catch message in chrome console what $scope.engagementTimeMix and $scope.qualityScoreByTimeMix it not a function


回答1:


You can use ng-if and draw graph when you already have value
example:

<div class="section" ng-if="qualityScoreByTimeMix">
    <highchart id="mix_quality_score_by_time" type="area" data="qualityScoreByTimeMix" chart-style="qualityScoreMixChartStyle"></highchart>
</div>


来源:https://stackoverflow.com/questions/35201724/angularjs-promise-in-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!