Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在$http.get内迭代_Angularjs_Http - Fatal编程技术网

Angularjs 在$http.get内迭代

Angularjs 在$http.get内迭代,angularjs,http,Angularjs,Http,我有一段代码,每个请求显示20个项目 .controller('MovieController',function ($scope,$http) { $scope.movies = $http.get(https://api.themoviedb.org/3/movie/popular&page=1) .then( function (response) { $scope.movies = response

我有一段代码,每个请求显示20个项目

.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;

            }

       );
})

如果我把page=2,我将得到另一组20项,以此类推,而不是page=1。如果我希望在一个页面中有多个get请求,如何在$http.get中进行迭代?我想在显示第一个20项之后再显示第二个20项。

有一种将回调函数链接在一起以获得所需结果的经典方法。或者有更好的方法,使用$q.all。您应该使用$q.all一次发送多个http请求

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); 
    }
})
可能重复的