Answer the question
In order to leave comments, you need to log in
Angular - promise in a loop, and how to get data on each iteration?
Good afternoon! Tell me, please, how is it possible with such a code, in the controller to receive data from the service at each iteration.
Routing
'profiles': {
controller: 'profilesList',
templateUrl: '/partials/profiles/profiles.list.html',
resolve: {
profiles: function(dataservice) {
return dataservice.getProfiles();
}
}
}
function getProfiles() {
var promises = _.range(pages).map(function(page) {
var deffered = $q.defer();
$http
.get('http://api/v1/profiles?page=' + page)
.success(function(data) {
// тут мое непонимание
deffered.resolve(data);
})
.error(function(error) {
deffered.reject();
});
return deffered.promise;
});
$q.all(promises).then(function() {
//
});
}
Answer the question
In order to leave comments, you need to log in
On the server to give pages. Loop through pages in Angular. Then just add your next profile page to the end of the array. For example like this:
function() {
return $http.get(''http://api/v1/profiles?page=' + page).then(function (result) {
$scope.page += 1;
return angular.forEach(result.data, function (item) {
return $scope.profiles.push(item);
});
});
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question