iterating inside a $http.get - angularjs

I've this block of code which displays 20 items per request.
.controller('ActorController',function($scope,$http) {
$scope.getActors = function () {
var actors = $http.get(https://api.themoviedb.org/3/person/popular&page=1);
actors.then(
function (response) {
$scope.actors = response.data.results;
}
);
}
})
Instead of page=1, if i put page=2, I'll get another set of 20 items and so on. How to iterate inside the $http.get if I want more than 1 get request in a single page? I want to display the 2nd 20 items after displaying the 1st 20.

So you're saying that you want to make multiple calls to $http.get in a loop? If so you'd do something like this:
.controller('ActorController',function($scope,$http) {
$scope.getActors = function () {
for(var i = 0; i < 5; i++) {
getPage(i);
}
}
function getPage(i) {
var actors = $http.get('https://api.themoviedb.org/3/person/popular&page=' + i);
actors.then(function (response) {
for(var j = 0; j < response.data.results.length; j++) {
$scope.actors.push(response.data.results[j]);
}
});
}
})
That will put the data for the first 5 pages into the $scope.actors array.

you can use $q.all to send multiple http requests at once
var fulAr = [];
for (var i = 1; i <= 2; i++) {
fulAr.push($http.get("https://api.themoviedb.org/3/person/popular&page=" + i))
}
$q.all(fulAr).then(function(response) {
console.log(response[0].data) //page 1 content
console.log(response[1].data) //page 2 content
for(var j = 0; j < response.length; j++) {
$scope.actors = $scope.actors.concat(response[j].data.results);
}
})
Make sure to inject $q to controller

Related

Console returning baddata

I cannot figure out why this http post is returning bad data in console :-
var dataObj = {"id": 21};
$http.post('http://localhost:9099/GetItems', dataObj).then(function(response) {
var size = response.data.length;
for (var i = 0; i < size; i++) {
var list = response.data.split(",");
for (var j = 0; j < list.length; j++) {
var elem = angular.element("#" + s);
elem.style.visible = "block";
}
}
});
I changed GetItems to a different api - this had no problem.
Looks like you are trying to get data from back end belongs to the id.
To get items use get instead of post
Like $http.get('http://localhost:9099/GetItems' + id)

Loading of paginated data from API

I am working on a system that currently requires me to load all items from an API.
The API is built with pagination feature in it. I keep calling the API a number of times and $http.get cursing the system not to respond. For example, once I load the page that needs to call the API many times (like 50 to 80 times depending on the number of pages), for a few minutes anything I do won't respond until the calling of the API is almost finished. I already tried a lot of ways but it won't work.
$scope.loadAllPagedItems = function (category_uuid, max_pageitem, item_perpage) {
for (var a = 0 ; a < max_pageitem; a++) {
itemResource.findItems(category_uuid, item_perpage, a).then(function (response) {
if (response.data.data.length > 0) {
for (var a = 2 ; a < $scope.categories.length; a++) {
if ($scope.categories[a][0].category_uuid == response.data.data[0].category_uuid) {
for (var j = 0; j < response.data.data.length; j++) {
$scope.categories[a][0].data.push(response.data.data[j]);
}
}
}
}
}, function (error) {
console.log(error);
})
}
}
Is there any way I can do this better?
Sequentially Retrieving Paginated Data from an Asynchronous API
$scope.loadAllPagedItems = function(category_uuid, max_pageitem, item_perpage) {
var promise = $q.when([]);
for (let a = 0 ; a < max_pageitem; a++) {
promise = promise
.then(function(dataArray) {
var p = itemResource.findItems(category_uuid, item_perpage, a);
p = p.then(function (response) {
return dataArray.concat(response.data.data);
});
return p;
});
};
return promise;
};
The above example, executes XHRs sequentially and uses the array concat method to merge the resolved arrays into a single array.
Usage:
$scope.loadAllPagedItems(category, pages, itemsPerPages)
.then(function(finalArray) {
console.log(finalArray);
}).catch(function(response) {
console.log("ERROR ",response.status);
throw response;
});

Iterating inside $http.get

I've this block of code which displays 20 items per request.
.controller('MovieController',function ($scope,$http) {
$scope.movies = $http.get(https://api.themoviedb.org/3/movie/popular&page=1)
.then(
function (response) {
$scope.movies = response.data.results;
}
);
})
Instead of page=1, if i put page=2, I'll get another set of 20 items and so on. How to iterate inside the $http.get if I want more than 1 get request in a single page? I want to display the 2nd 20 items after displaying the 1st 20.
There is the classic way of chaining callback functions together to achieve the desired result. Or there is the better way, which uses $q.all().You should use $q.all to send multiple http requests at once
var reqAr = [];
for (var i = 0; i <= 1; i++) {
reqAr.push($http.get("https://api.themoviedb.org/3/movie/popular&page=" + i))
}
$q.all(reqAr).then(function(response) {
console.log(response[0].data) //page 1 content
console.log(response[1].data) //page 2 content
for(var j = 0; j < response.length; j++) {
$scope.movies = $scope.movies.concat(response[j].data.results);
}
})

How can I push element in an array whenever a function get called?

I am implementing infinite scroll. I Have an array named hotels which gets data from $http.get() method. Now I want to populate a new array named hotelsNew[] with the value of hotels array. But I want to push value in the hotelsNew incrementing value of m and j.
The value of m and j need to be updated (increment) whenever function loadMore() is called. Initial value of m is 0 and j is 5. When loadMore() gets called, again m will be 5 and j will be 10 and so on.
I am unable to save value of m and j anywhere because $http.get() is asynchronous. Is there any proper way to save values of m and j, and reuse it when loadMore() gets called again?. Maximum j value is 40. and when m = j(40), it will stop.
Here is my controller code.
$scope.loadMore = function() {
$http.get(url).success(function(data, status, headers, config) {
if(data) {
$scope.hotels = data.hotels;
for(var i = m; i < j; i++) {
console.log('i=',i);
$scope.hotelsNew.push($scope.hotels[i]);
console.log('hotelsNew',$scope.hotelsNew);
}
}
}
How can i increment m and j value ?any idea?
If m and j increments same amount each time, you can create a local variable in your controller.
var m = 0, j = 5;
$scope.loadMore = function() {
$http.get(url).success(function(data, status, headers, config) {
if(data && j < 40) {
$scope.hotels = data.hotels;
for(var i = m; i < j; i++) {
console.log('i=',i);
$scope.hotelsNew.push($scope.hotels[i]);
console.log('hotelsNew',$scope.hotelsNew);
}
m += 5;
j += 5;
}
}
}
Use a promise to avoid async call
$scope.loadMore = function() {
$http.get(url).then(function(data) {
if(data) {
$scope.hotels = data.hotels;
for(var i = m; i < j; i++) {
console.log('i=',i);
$scope.hotelsNew.push($scope.hotels[i]);
console.log('hotelsNew',$scope.hotelsNew);
}
}
}
see the example for pushing array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
get length of data using
$s=Array.sizeof($data)
for($i<0;$i<=$s;$i++){//task here}

Name database child in Firebase?

This is the code I have.
function saveTrip()
{
routeData.clear();
for (var i = 0; i < dirDisplay.directions.routes[0].legs.length; i++) {
for (var j = 0; j < dirDisplay.directions.routes[0].legs[i].steps.length; j++) {
routeData.push(dirDisplay.directions.routes[0].legs[i].steps[j].path);
}
}
routeLinesRef.push(routeData, function(error){
if (error) {
$('#savedSpan').html('Data could not be saved.' + error);
} else {
$('#savedSpan').html('Data saved successfully!');
}
});
}
Array.prototype.clear = function() {
this.splice(0, this.length);
};
routeLinesRef.limit(10).on('child_added', function(snapshot)
{
// loop over each route we get from firebase
route = snapshot.val();
What function should be written to write into the hierarchy as shown in the image?
id = snapshot.name();
// make an array that is initially blank
// It will contain all the latitude and longitudes of each point on in the route
var routeCoordinates = [];
// This loops over each point on the route
for (var i=0; i<route.length; i++)
{
for (var j in route[i])
{
if (j==0 && i>0)
{
continue
}
else
{
This part just takes each point on the route, and converts it into
a google maps LatLng object. For example, if the data is [58.23, 18.8], it will do:
new google.maps.LatLng(58.23, 18.8) to it, which turns it into a format that google maps likes.
if (route[i][j].lb && route[i][j].mb) {
routeCoordinates.push(new google.maps.LatLng
(route[i][j].lb, route[i][j].mb));
//console.log(j + ' ' + snapshot.val()[route][i][j].lb, snapshot.val()[route][i][j].mb);
}
}
}
}
What should I do? I can't find any tutorial or anything.

Resources