I'm working with an API that contains pages from 1 to 10 and my goal is to cycle through these page numbers to make the necessary API calls.
app.factory('companies', ['$http', function($http) {
var i;
for (i = 1; i < 11; i++) {
var data = $http.get('https://examplepage.com/wp-json/wp/v2/categories?per_page=50&page=' + i);
console.log('list', data);
}
return data;
}]);
After making all 10 API calls, I logged the data and you can see the results in this JSON data image.
However, when I try to display the list of names received from all 10 API calls, it seems like only the data from the last call is being used. How can I merge all the returned data into one object so I can display a complete list of names from pages 1 to 10?
app.controller('HomeController', ['$scope', 'companies', function($scope, companies) {
companies.success(function(data) {
$scope.companies = data;
console.log('companies', $scope.companies);
});
}]);
In the view.html file:
<div class="container" ng-controller="HomeController">
<div ng-repeat="company in companies" class="list">
<a href="#/{{ company.id }}" class="company-name">{{ company.name }}</a>
</div>
</div>