问题
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