简体   繁体   English

Angular中的多个HTTP请求

[英]Multiple HTTP requests in Angular

So this my controller: 所以这是我的控制器:

app.controller('dbCtrl', function($scope, $http) {
$http.get("http://private-abc.apiary-mock.com/bus")
.success(function(response) {
 $scope.network = response.networkupdates;});
 });

What I wanted to do next is call a 2nd HTTP request, I guess in terms of best practice would it be best to create a 2nd controller to call the 2nd HTTP or would it be best to include the 2nd HTTP call in this current controller (and if so, how?) Thanks. 我接下来要做的是调用第二个HTTP请求,按照最佳实践,我猜想最好是创建一个第二个控制器来调用第二个HTTP,或者最好在当前控制器中包含第二个HTTP调用(如果是的话,如何?)谢谢。

So one of the cool aspects of using promises is that they can be chained. 因此,使用承诺的一个很酷的方面就是可以将它们链接在一起。 So in your case, you are calling: 因此,就您而言,您正在打电话:

$http.get("http://private-abc.apiary-mock.com/bus")

Which returns a promise that you can then chain to another promise like so: 它返回一个承诺,然后您可以将其链接到另一个承诺,如下所示:

var requests = $http.get("http://private-abc.apiary-mock.com/bus").then(function(response) {
    $scope.network = response.networkupdates;
    // make second get call and return it to chain the promises
    return $http.get("some-other-endpoint").then(function(otherResponse) {
        // you can do something here with the response data
        return otherResponse;
    });
});

What you have now is two chained promises that will return the final value, so if you call this later: 您现在拥有的是两个链接的诺言,这些诺言将返回最终值,因此,如果稍后调用它:

requests.then(function(otherResponse) {
    // or you can do something here
    doSomething(otherResponse);
});

As far as best practices for Angular, I would say you are better off creating a service or factory to handle any and all http requests. 至于Angular的最佳实践,我想说您最好创建一个服务或工厂来处理所有HTTP请求。 Controllers are really just meant to bind data to the view; 控制器实际上只是用来将数据绑定到视图。 services are where your business logic and data population should happen. 服务是应该进行业务逻辑和数据填充的地方。

You can make your $http calls in the same controller. 您可以在同一控制器中进行$http调用。

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error . $http服务是一个带有单个参数(配置对象)的函数,该参数用于生成HTTP请求,并通过两个$ http特定方法返回成功的promisesuccess and error It internally uses $q (a promise/deferred implementation inspired by Kris Kowal's Q). 它在内部使用$q (受Kris Kowal Q启发的承诺/延期实现)。

If your two $http are independent of each other you use the $q.all to "join" the results of your http calls. 如果两个$http彼此独立,则可以使用$q.all来“ $q.all ” http调用的结果。

Example : 范例:

$q.all([
    $http.get("http://private-abc.apiary-mock.com/bus"),
    $http.get('/someUrl')
  ]).then(function(results) {
     $scope.network = results[0];
     $scope.whatevername= results[1]

  });
}

If your http calls are dependent on one another then you can use the concept of chaining . 如果您的http调用相互依赖,则可以使用chaining的概念。

for example: 例如:

$http.get("http://private-abc.apiary-mock.com/bus").then(function(result) {
    $scope.network = result.networkupdates;    
    return $http.get("someurl").then(function(res) {        
        return res;
    });
});

For refernce of q you can see https://github.com/kriskowal/q 有关q引用,请参见https://github.com/kriskowal/q

For refernce of $q service you can see https://docs.angularjs.org/api/ng/service/ $q 有关$q service引用,请参见https://docs.angularjs.org/api/ng/service/ $ q

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM