This is my way:
var activeArr = [];
var activeDate = [];
var day = (endDate - startDate) / (24 * 60 * 60 * 1000);
for (var i = 1; i < day + 1; i++) {
activeDate.push(endDate - (24 * 60 * 60 * 1000) * i);
var start = endDate - (24 * 60 * 60 * 1000) * i;
var end = endDate - (24 * 60 * 60 * 1000) * (i - 1);
statisService.getRegStatis(start, end).then(function(data) {
activeArr.push(data);
if(activeArr.length == day){
var active = [];
for(var j=0;j<activeArr.length;j++){
var obj = {};
obj.date = activeDate[j];
obj.data = activeArr[j].data;
active.push(obj);
}
$scope.active = active;
}
});
}
the service:
userServiceModule.factory('statisService', ['$http', 'serverUrl', function($http, serverUrl) {
return {
getRegStatis: function(startDate, endDate) {
var url = serverUrl + "/adminDA/dbReport?startTime=" + startDate + "&endTime=" + endDate;
return $http.get(url).then(function(result) {
return result.data;
});
}
};
I want to put the date and the data into one object so that I can use it in the view like this:
<tr ng-repeat="item in active track by $index">
The for loop will not wait for all the service calls to be completed,So I deal the data in the for loop,and I think this is not a good way,but I don't know how to do it better.