简体   繁体   English

使用angularjs使用Restful API时如何获取多个$ http

[英]How to get multiple $http when Consume Restful APIs with angularjs

I try to get from JSONPlaceholder data to display, and I need more than one $http.get , this is my code. 我尝试从JSONPlaceholder数据获取显示,并且我需要多个$http.get ,这是我的代码。 The problem is, that from the second call I don't get any data 问题是,从第二次通话开始,我没有任何数据

  MyAlbum1.controller('albumList', function($scope, $http) {
    $http.get('http://jsonplaceholder.typicode.com/albums').
        then(function(response) {
            $scope.albumDetails = response.data;
        });

    $http.get('http://jsonplaceholder.typicode.com/users').
        then(function(response) {
            $scope.albumUsers = response.data;
       });
});

If I try for example to display in the partial view albumUsers.name it will not display. 例如,如果我尝试显示在部分视图albumUsers.name ,它将不会显示。 Only if I put first the $http.get('http://jsonplaceholder.typicode.com/users'). 仅当我首先放置$http.get('http://jsonplaceholder.typicode.com/users'). but then it will not display the albumDetails.id 但是它将不会显示albumDetails.id

So how do I get more than one http.get ? 那么,如何获得多个http.get

Thanks 谢谢

You could load 1 after another: 您可以一个接一个地加载:

MyAlbum1.controller('albumList', function($scope, $http) {

     $http.get('http://jsonplaceholder.typicode.com/albums').
     then(function(response) {
        $scope.albumDetails = response.data;


        $http.get('http://jsonplaceholder.typicode.com/users').
        then(function(response) {
                $scope.albumUsers = response.data;
        });
    });
});

or 要么

  MyAlbum1.controller('albumList', function($scope, $http, $q) {
       $q.all([$http.get('http://jsonplaceholder.typicode.com/albums'), $http.get('http://jsonplaceholder.typicode.com/users')]).then(function(response) { 
       });
  });

use $q to send multiple requests at once.first inject the $q to controller. 使用$q发送多个请求。首先将$q注入控制器。

MyAlbum1.controller('albumList', function($scope, $http,$q) {    

    $q.all(['http://jsonplaceholder.typicode.com/albums','http://jsonplaceholder.typicode.com/users',]).then(function(response){
         $scope.albumDetails = response[0].data;
         $scope.albumUsers= response[1].data;
    })    
})

Thanks! 谢谢! Work now. 在工作,在忙。 Here's the code I'm using: 这是我正在使用的代码:

MyAlbum.controller('albumList', function($scope, $http, $q) {
$http.get('http://jsonplaceholder.typicode.com/albums').
    then(function(response) {
        $scope.albumDetails = response.data;
    });

$http.get('http://jsonplaceholder.typicode.com/users').
    then(function(response) {
        $scope.albumUsers = response.data;
   });

$q.all(['http://jsonplaceholder.typicode.com/albums','http://jsonplaceholder.typicode.com/users',]).
    then(function(response){
        $scope.albumDetails = response[0].data;
        $scope.albumUsers= response[1].data;
});  

}); });

But, when call the data, I need to do as follow: 但是,在调用数据时,我需要执行以下操作:

User: {{albumUsers[1].name}}

Like, I need the [1] in order that it will work. 就像,我需要[1]才能起作用。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用angularjs使用Restful API时如何使用多个$ http对数据进行排序和匹配 - How to sort and match data with multiple $http when Consume Restful APIs with angularjs 如何使用WebSockets和JSON / RESTful API的AngularJS - How to AngularJS with WebSockets and JSON/RESTful APIs 如何在angularjs中使用$ resource消耗Spring Restful Web服务 - how to consume spring restful web service using $resource in angularjs 如何通过分页使用RESTful API来按顺序获取AngularJS中的所有记录 - How to consume a RESTful API with pagination to fetch all records sequentially in AngularJS 包括外部API时如何测试AngularJS? - How to test AngularJS when including external APIs? 如何交流使用C编程开发的AngularJS的$ http路由和API - How to communicate AngularJS's $http routes and APIs developed in C Programming AngularJS:如何使用令牌安全性和自定义方法处理RESTful API? - AngularJS: How do I handle RESTful APIs with token security and custom methods? 在angularjs中进行单元测试时,如何从http请求中获取参数? - How to get the params from an http requests when unit testing in angularjs? 使用Angularjs进行$ http GET时如何避免在URL中显示参数 - How to avoid displaying parameters in the URL when doing an $http GET with Angularjs AngularJs-如何获得$ http结果 - AngularJs - how to get $http result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM