在$ http.get中迭代 [英] iterating inside a $http.get

查看:126
本文介绍了在$ http.get中迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  .controller('ActorController',function( $ scope,$ http){

$ scope.getActors = function(){
var actors = $ http.get(https://api.themoviedb.org/3/person/ (
function(response){
$ scope.actors = response.data.results;
}
);
actors.then(
function(response)

))

而不是页面= 1,如果我把页面= 2,我会得到另一组20件物品等。如果我想在单个页面中获取多个请求,如何在$ http.get中迭代?我想在显示第一个20后显示第二个20项。

解决方案

所以你说你想多重在循环中调用 $ http.get >?如果是这样的话,你可以这样做:

  .controller('ActorController',function($ scope,$ http){ 

$ scope.getActors = function(){
for(var i = 0; i< 5; i ++){
getPage(i);
}
}

函数getPage(i){
var actors = $ http.get('https://api.themoviedb.org/3/person/popular&page= '+ i);
actors.then(function(response){
for(var j = 0; j $ scope。 actors.push(response.data.results [j]);
}
});
}
})
pre>

这会将前5页的数据放入 $ scope.actors 数组中。


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.

这篇关于在$ http.get中迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆