I have this API that has pages 1-10 and I want to loop through the page numbers to make the 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;
}]);
This is what I get when I console log the data for all 10 API calls
JSON data
My attempt to display all of the data (list of names), but it seems as though it's only taking the last API call and displaying it. How do I combine all of the returned data into one object to display the list of name from pages 1-10?
app.controller('HomeController', ['$scope', 'companies', function($scope, companies) {
companies.success(function(data) {
$scope.companies = data;
console.log('companies', $scope.companies);
});
}]);
view.html
<div class="container" ng-controller="HomeController">
<div ng-repeat="company in companies" class="list">
{{ company.name }}
</div>
</div>
Each call to the $http service returns a promise. You need to use $q.all to consolidate the promises:
app.factory('companies', function($http,$q) {
return { tenPagesPromise: tenPagesPromise };
function tenPagesPromise () {
var indices = Array.from({length:10}).map((x,i)=>i);
var promises = indices.map(i=>pagePromise(i));
return $q.all(promises).then(responseArray => {
var dataArray = responseArray.map(x=>x.data);
return dataArray.reduce((t,x)=>t.concat(x),[]);
});
}
function pagePromise(i) {
var url = "https://examplepage.com/wp-json/wp/v2/categories";
var params = { per_page: 50, page: i };
var config = { params: params }
promise = $http.get(url,config);
return promise;
}
});
Usage:
companies.tenPagesPromise.then(data => {
$scope.companies = data;
}).catch(function(errorResponse) {
console.log(errorResponse);
});
For more information, see AngularJS $q Service API Reference - all.
You need to resolve the promise and then add the data to an array, something like this:
app.factory('companies', ['$http', function($http) {
data = []
for (let i = 1; i < 11; i++) {
$http.get('https://examplepage.com/wp-json/wp/v2/categories?per_page=50&page=' + i)
.then( function(resp) {
console.log(resp.data);
data.push(resp.data);
})
}
return data;
}]);
Related
Here I created sample for services call which is Working fine Call one by one Link for sample.
like this:-
JSONService.getJSON('file.json').then(function(data){
$scope.languages = data;
});
JSONService.getJSON('file1.json').then(function(data){
$scope.languages1 = data;
});
I don't want to send separately. why because if its only two three call means fine.but
I need to do more than 100 calls. that time I can't do one by one.
so tried like this
JSONService.getJSON('file.json,file1.json').then(function(data){
$scope.languages = data[0];
$scope.languages1 = data[1];
});
In services use split the values and try to push the promise one by one and return as array its not working I don't know where i did mistake can any one help me on this..
app.service('JSONService', function($http){
var data = [];
return{
getJSON: function(url){
var parameter=url.split(',');
for(var i=0; i< parameter.length; i++){
$http.get(parameter[i])
.then(function(response){
data.push(response);
});
}
return data;
}
};
});
Link for Sample 2 not working
you need to use promises for that. here is the service you need to use
app.service('JSONService', function($http, $q){
var data = [];
return{
getJSON: function(url){
var urls = url.split(','),
promises = [];
for(var i=0; i< urls.length; i++){
var inPromise = $http.get(urls[i])
.then(function(response){
data.push(response.data);
});
promises.push(inPromise);
}
//return the promise from the $q.all, that makes sure, that all pushed promises are ready and return the chapters.
return $q.all(promises).then(function () {
return data;
});
}
};
});
Also updated your plnkr http://plnkr.co/edit/smMv9jPyMRNYX2AzAuRC?p=preview. This concatenates all the result to one array object languages
$q
This is what $q is for.
app.service("JSONService", ["$http", "$q", function($http, $q){
return {
getJson: function(callback){
_urls = ["data1.json", "data2.json", "data3.json"],
_urlCalls = [],
_data = [];
angular.forEach(_urls, function(url) {
_urlCalls.push($http.get(url));
});
$q.all(_urlCalls).then(
function(results) {
results.forEach(function(e,i){
_data.push(e.data);
});
callback(_data);
},
function(errors) {},
function(updates) {}
);
}
}
}]);
Usage
From the controller.
JSONService.getJson(function(response){
console.log(response);
}
I'm just learning angularjs and I'm strugling to get the results I want from this factory :
app.factory('foreCast', ['$http', function($http,$scope ) {
var req = $http.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK");
req.success(function (data) {
req.rows =data.results.collection1;
req.rand =req.rows[Math.floor(Math.random() * req.rows.length)];
console.log(req.rand);
//i want this results
});
return req; //how can i return req.rand?
}]);
app.factory('foreCast', ['$q', '$http', function ($q, $http) {
return {
getRand: function () {
var deferred = $q.defer();
$http
.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK")
.then(function (response) {
var rows = response.data.results.collection1;
var rand = rows[Math.floor(Math.random() * rows.length)];
deferred.resolve(rand);
}, function (err) {
deferred.reject(err);
})
return deferred.promise;
}
}
}])
And then from controller for exemple use it like this :
foreCast.getRand().then(function(rand){
var myRand = rand;
})
So first of all, you never want to use the success handler with http. I believe its actually getting deprecated. The best practice is to use .then and .catch for errors. Secondly, most of the time you should be processing the result in your controller since the service is not supposed to know what to do with the data.
So that being said, we can trim down your factory a little bit.
app.factory('foreCast', ['$http', function($http,$scope ) {
var factory = {};
factory.rand = function() {
return $http.jsonp("https://www.kimonolabs.com/api/c1ab8xnw?&apikey=api-key&callback=JSON_CALLBACK");
};
return factory;
}]);
Now in your controller:
app.controller('controller', ['foreCast', function(foreCast) {
var self = this;
foreCast.rand().then(function(response) {
var rows = response.results.collection1;
self.rand = rows[Math.floor(Math.random() * rows.length)];
});
}]);
now in your view just access this value with rand.
<div ng-bind="ctrl.rand"></div>
In my main module I want to create a service that parses a json file and pushes the contents to an array, after I would like service to have the array be returned so it is easily accessible by any of the controllers. The issue is the function is running before the $http request is complete so it always returns an empty array
dashModule.factory('dataFetch', function($http) {
var emailArray = [];
$http.get('../data/emails.json').success(function log(obj) {
for (var i = 0; i < obj.length; i++) {
emailArray[i] = obj[i];
}
});
return {
test: function() {
return emailArray;
}
};
});
use promise, like:
dashModule.factory('dataFetchService', function($http) {
var myReq = function() {
return $http.get('../data/emails.json').then(function(response){
return response.data;
});
};
return { myReq: myReq };
});
and
function getMyReq($scope, dataFetchService) {
var myReqPromise = dataFetchService. myReq();
myReqPromise.then(function(result) {
console.log(result);
});
}
I have an factory that gets data from my backend:
as.factory("abbdata", function GetAbbData($http,$rootScope,$routeParams,$q) { //$q = promise
var deffered = $q.defer();
var data = [];
var abbdata = {};
abbdata.async = function () {
$http.get($rootScope.appUrl + '/nao/summary/' + $routeParams['id']).success(function(d) {
data = d.abbData;
deffered.resolve();
});
return deffered.promise;
};
abbdata.data = function() {
return data;
};
return abbdata;
});
A call my factory like this in my controller:
abbdata.async().then(function() {
$scope.abbData = abbdata.data(); //Contains data
});
When I do a console.log($scope.abbData) outside my service call, just underneath, the result Is undifined. Why? Should not the $scope.abbData contain the data from my service after I call it?
EDIT:
You need to pass the data that should be returned into the resolve function like this:
deffered.resolve(data);
EDIT:
To get the data in the controller do this:
abbdata.async().then(function(data) {
$scope.abbData = data; //Contains data
});
Why don't you simply return that value from the async call in the first place?
You can chain promises so by attaching a success handler in your factory and returning a value from that you can simplify your code to:
as.factory("abbdata", function GetAbbData($http,$rootScope,$routeParams) {
return {
async: function () {
return $http.get($rootScope.appUrl + '/nao/summary/' + $routeParams['id']).success(function(d) {
return d.data.abbData;
});
}
}
});
And then use it like
abbdata.async().then(function(data) {
$scope.abbData = data; //Contains data
});
if you console.log($scope.abbData) outside the service call it should show undefined, since the call is asynchronous.
abbdata.async().then(function() {
$scope.abbData = abbdata.data(); //Contains data
});
console.log($scope.abbData) // this should show undefined
The console.log($scope.abbData) just after setting the abbData should show the data
abbdata.async().then(function() {
$scope.abbData = abbdata.data(); //Contains data
console.log($scope.abbData) // this should show the data
});
EDIT
you can use abbData from your service call like for example
angular.module('myApp', []).controller('HomeCtrl', function($scope, abbdata){
var updateUI;
$scope.abbData = [];
abbdata.async().then(function() {
$scope.abbData = abbdata.data(); //Contains data
updateUI();
});
updateUI = function(){
//do something with $scope.abbData
}
});
EDIT 2
On response to your query, I would do something like,
angular.module('myApp', [])
.controller('JobsCtrl', function($scope, $jobService) {
$scope.jobs = [];
$jobService.all().then(function(jobs) {
$scope.jobs = jobs;
});
})
.service('$jobService', function ($q, $http) {
return {
all: function () {
var deferred = $q.defer();
$http({
url: 'http://url',
method: "GET"
}).success(function (data) {
deferred.resolve(data);
}).error(function () {
deferred.reject("connection issue");
});
return deferred.promise;
}
}
});
associated view
<body ng-app = "myApp">
<div ng-controller = "JobsCtrl">
<div ng-repeat="job in jobs track by job.id">
<a href="#/tab/jobs/{{job.id}}" class="item item-icon-right">
<h2>{{job.job_name}}</h2>
<p>DUE DATE: {{job.job_due_date}}</p>
</a>
</div>
<div>
</body>
Here the service an all function which returns a promise, i.e. it will notify when data is fetched.
in the controller the service is called and as soon the service call is resolved the $scope.jobs is assigned by the resolved data.
the $scope.jobs is used in the angular view. as soon as the jobs data are resolved, i.e. $scope.jobs is assigned, the view is updated.
hope this helps
I had a quick look, I have 2 ideas:
First theory: your service is returning undefined.
Second theory: you need to run $scope.$apply();
See this fiddler: https://jsfiddle.net/Lgfxtfm2/1/
'use strict';
var GetAbbData = function($q) {
//$q = promise
var deffered = $q.defer();
var data = [];
var abbdata = {};
abbdata.async = function () {
setTimeout(function() {
//1: set dummy data
//data = [200, 201];
//2: do nothing
//
//3: set data as undefined
//data = undefined;
deffered.resolve();
}, 100);
return deffered.promise;
};
abbdata.data = function() {
return data;
};
return abbdata;
};
var abbdata = GetAbbData(Q)
abbdata.async().then(function() {
console.log(abbdata.data()); //Contains data
});
I have stripped away a lot of dependencies and replaced $q with Q just for my own ease.
In the above example, I first attempted to run the code with dummy data, the console output the expected data, then I tried to not assign the data, and I get an empty array. This is why I assume that if you are seeing 'undefined' you must be explicitly setting the value to 'undefined'.
That aside, I also noticed that you were testing the result by reading directly from $scope. I know that when not inside the angular scope, doing operations on the $scope object does not necessarily happen in a timely manner, and typing $scope.$apply() usually fixes this. Usually, when using $http, angular keeps you in the appropriate scope, but you are creating your own promise using $q so this could be another potential issue.
Finally, the other two answers have pointed out that you are not using promises in the standard way. Although your code works fine, it is not normal to set your data directly onto your service and retrieve it from there. You can keep your service stateless by simply resolving your promise with the data that you want to process in the then method as shown by the answers by Anzeo and Markus.
I hope I was able to find the solution, good luck.
Dipun
as.factory("abbdata", function GetAbbData($http,$rootScope,$routeParams,$q) { //$q = promise
var deffered = $q.defer();
var data = [];
var abbdata = {};
abbdata.async = function () {
$http.get($rootScope.appUrl + '/nao/summary/' + $routeParams['id']).success(function(d) {
data = d.abbData;
deffered.resolve(data);
});
return deffered.promise;
};
abbdata.data = function() {
return data;
};
return abbdata;
});
I'm new to AngularJS and am still trying to wrap my head around using services to pull data into my application.
I am looking for a way to cache the result of a $http.get() which will be a JSON array. In this case, it is a static list of events:
[{ id: 1, name: "First Event"}, { id: 2, name: "Second Event"},...]
I have a service that I am trying to use to cache these results:
appServices.service("eventListService", function($http) {
var eventListCache;
this.get = function (ignoreCache) {
if (ignoreCache || !eventListCache) {
eventListCache = $http.get("/events.json", {cache: true});
}
return eventListCache;
}
});
Now from what I can understand I am returning a "promise" from the $http.get function, which in my controller I add in a success callback:
appControllers.controller("EventListCtrl", ["$scope", "eventListService",
function ($scope, eventListService) {
eventListService.get().success(function (data) { $scope.events = data; });
}
]);
This is working fine for me. What I'd like to do is add an event to the eventListService to pull out a specific event object from eventListCache.
appServices.service("eventListService", function($http) {
var eventListCache;
this.get = function (ignoreCache) { ... }
//added
this.getEvent = function (id) {
//TODO: add some sort of call to this.get() in order to make sure the
//eventListCache is there... stumped
}
});
I do not know if this is the best way to approach caching or if this is a stupid thing to do, but I am trying to get a single object from an array that may or may not be cached. OR maybe I'm supposed to call the original event and pull the object out of the resulting array in the controller.
You're on the right track. Services in Angularjs are singeltons, so using it to cache your $http request is fine. If you want to expose several functions in your service I would do something like this. I used the $q promise/deferred service implementation in Angularjs to handle the asynchronus http request.
appServices.service("eventListService", function($http, $q) {
var eventListCache;
var get = function (callback) {
$http({method: "GET", url: "/events.json"}).
success(function(data, status) {
eventListCache = data;
return callback(eventListCache);
}).
}
}
return {
getEventList : function(callback) {
if(eventListCache.length > 0) {
return callback(eventListCache);
} else {
var deferred = $q.defer();
get(function(data) {
deferred.resolve(data);
}
deferred.promise.then(function(res) {
return callback(res);
});
}
},
getSpecificEvent: function(id, callback) {
// Same as in getEventList(), but with a filter or sorting of the array
// ...
// return callback(....);
}
}
});
Now, in your controller, all you have to do is this;
appControllers.controller("EventListCtrl", ["$scope", "eventListService",
function ($scope, eventListService) {
// First time your controller runs, it will send http-request, second time it
// will use the cached variable
eventListService.getEventList(function(eventlist) {
$scope.myEventList = eventlist;
});
eventListService.getSpecificEvent($scope.someEventID, function(event) {
// This one is cached, and fetched from local variable in service
$scope.mySpecificEvent = event;
});
}
]);
You are on the right track. Here's a little help:
appServices.service("eventListService", function($http, $q) {
var eventListCache = [];
function getList(forceReload) {
var defObj = $q.defer(), listHolder;
if (eventListCache.length || forceReload) {
listHolder= $http.get("/events.json", {cache: true});
listHolder.then(function(data){
eventListCache = data;
defObj.resolve(eventListCache);
});
} else {
defObj.resolve(eventListCache);
}
return defObj.promise;
}
function getDetails(eventId){
var defObj = $q.defer();
if(eventId === undefined){
throw new Error('Event Id is Required.');
}
if(eventListCache.length === 0){
defObj.reject('No Events Loaded.');
} else {
defObj.resolve(eventListCache[eventId]);
}
return defObj.promise;
}
return {
eventList:getList,
eventDetails:getDetails
};
});
Then, in your controller, you handle it like this:
appControllers.controller("EventListCtrl", ["$scope", "eventListService",
function ($scope, eventListService) {
var eventList = eventListService.getList();
eventList.then(function(data){
$scope.events = data;
});
$scope.getEventsList = function(reloadList){
eventList = eventListService.getList(reloadList);
eventList.then(function(data){
$scope.events = data;
});
};
$scope.getEventDetails = function(eventID){
var detailsPromise = eventListService.getDetails(eventID);
detailsPromise.then(function(data){
$scope.eventDetails = data;
}, function(reason){
window.alert(reason);
});
}
}
]);
This way, your events are loaded when the controller first loads, and then you have the option to request a new list by simply passing in a boolean. Getting event details is also handled by an internal promise to give you some error handling without throwing a disruptive error.