9094US
9094US

Reputation: 7

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.

Upvotes: 0

Views: 46

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222572

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); 
    }
})

Upvotes: 1

Related Questions