В этом случае у меня есть серьезная проблема. Я получаю данные от github API по этому коду ниже, но поскольку github alllow всего 30 результатов на страницу, я хотел бы получить все данные для более качественных опций сортировки и нажать на один массив объектов. Ниже вы найдете код, который отлично работает
$scope.getData = function () {
$http({
method: "GET",
url: api url + pageNum
}).then(function mySucces(response) {
$scope.mydata = response.data;
$scope.isLoading = false;
$scope.result = $scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; });
}, function myError(response) {
$scope.error = response.statusText;
});
};
Но я хотел бы сделать что-то вроде этого
$scope.getData = function () {
for(var i =0; i<= $scope.pages.length; i++){
$http({
method: "GET",
url: "apiUrl + i
}).then(function mySucces(response) {
$scope.mydata = response.data;
$scope.isLoading = false;
$scope.result = $scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; });
}, function myError(response) {
$scope.error = response.statusText;
});
};
Любые идеи для этого?
Вы должны проверить документацию $ q на сайте AngularJS.
$scope.getData = function () {
$scope.result = [];
$scope.error = [];
$q.all($scope.pages.map(function(page) {
return $http({
method: "GET",
url: "apiUrl" + i
})
// This function will be triggered when each call is finished.
.then(function mySucces(response) {
$scope.mydata.push(response.data);
$scope.result.push($scope.mydata.map(function (a) { return { 'name': a.name, 'html_url': a.html_url }; }));
})
}))
// This call will be called when all of the calls are finished.
.then(function(success) {
$scope.isLoading = false;
}).catch(function (error) {
$scope.error = response.statusText;
});
}