我承诺获取数据:
var activeUsersByZonePromise = function (zone) {
return $http.get(backendUri.buildAppUri('/active_users/total', {
'start_day': $scope.timeFilter.startDay(),
'zone': zone
})).then(function (data) {
return {
count: data.data.activeUsers.current,
sincePreviousTime: data.data.activeUsers.current - data.data.activeUsers.previous,
arrow: arrowImage(data.data.activeUsers.current, data.data.activeUsers.previous)
};
});
};
在此之后我会注意构建一些数据模型,并使用foreach来设置值并获取数据,如下所示:
getZonesPromise.then(function (result) {
var zones = [];
angular.forEach(result.qualityScore.zones, function (response) {
zones.push({
zone: response.zone,
qualityScore: response.qualityScore,
prevQualityScore: response.prevQualityScore
});
});
return zones;
}).then(function (zones) {
var data = [];
angular.forEach(zones, function (zone) {
data.push({
title: zone.zone,
activeUsers: activeUsersByZonePromise(zone.zone),
//...
$scope.zonesDataModel = data;
});
而activeUsersByZonePromise(zone.zone)无法获取数据,因为我使用的RegionsDataModel用于数据绑定,帮助我解决此问题。
答案 0 :(得分:2)
getZonesPromise.then(function (result) {
var zones = [];
angular.forEach(result.qualityScore.zones, function (response) {
zones.push({
zone: response.zone,
qualityScore: response.qualityScore,
prevQualityScore: response.prevQualityScore
});
});
return zones;
}).then(function (zones) {
$scope.zonesDataModel = [];
angular.forEach(zones, function (zone) {
activeUsersByZonePromise(zone.zone).then(function(user) {
$scope.zonesDataModel.push({
title: zone.zone,
activeUsers: user,
//...
}
});
您将保证activeUsersByZonesPromise
结果分配给activeUsers
。
这种方式似乎更好