Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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
Javascript 在Angular中有多个HTTP请求_Javascript_Angularjs_Rest - Fatal编程技术网

Javascript 在Angular中有多个HTTP请求

Javascript 在Angular中有多个HTTP请求,javascript,angularjs,rest,Javascript,Angularjs,Rest,这是我的控制器: app.controller('dbCtrl', function($scope, $http) { $http.get("http://private-abc.apiary-mock.com/bus") .success(function(response) { $scope.network = response.networkupdates;}); }); 接下来我想做的是调用第二个HTTP请求,我想从最佳实践的角度来看,最好创建第二个控制器来调用第二个HTTP,还是

这是我的控制器:

app.controller('dbCtrl', function($scope, $http) {
$http.get("http://private-abc.apiary-mock.com/bus")
.success(function(response) {
 $scope.network = response.networkupdates;});
 });
接下来我想做的是调用第二个HTTP请求,我想从最佳实践的角度来看,最好创建第二个控制器来调用第二个HTTP,还是最好在当前控制器中包含第二个HTTP调用(如果是,如何?)
谢谢。

所以使用承诺的一个很酷的方面是,它们可以被链接起来。因此,在您的情况下,您正在呼叫:

$http.get("http://private-abc.apiary-mock.com/bus")
它返回一个承诺,然后您可以链接到另一个承诺,如下所示:

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;
    });
});
您现在拥有的是两个链接承诺,它们将返回最终值,因此如果稍后调用:

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

至于Angular的最佳实践,我认为最好创建一个服务或工厂来处理所有http请求。控制器实际上只是将数据绑定到视图;服务是您的业务逻辑和数据填充应该发生的地方。

所以使用承诺的一个很酷的方面是它们可以链接起来。因此,在您的情况下,您正在呼叫:

$http.get("http://private-abc.apiary-mock.com/bus")
它返回一个承诺,然后您可以链接到另一个承诺,如下所示:

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;
    });
});
您现在拥有的是两个链接承诺,它们将返回最终值,因此如果稍后调用:

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

至于Angular的最佳实践,我认为最好创建一个服务或工厂来处理所有http请求。控制器实际上只是将数据绑定到视图;服务是您的业务逻辑和数据填充应该发生的地方。

您可以在同一控制器中进行
$http
调用

$http
服务是一个函数,它接受一个参数(一个配置对象),用于生成http请求并返回一个带有两个$http特定方法的
承诺:
成功和错误
。它在内部使用
$q
(受Kris Kowal的q启发的承诺/延迟实现)

如果两个
$http
彼此独立,则使用
$q.all
来“加入”http调用的结果

例如:

$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]

  });
}
如果http调用相互依赖,则可以使用
链接的概念

例如:

$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;
    });
});
有关
q
的参考,您可以看到


有关
$q服务的参考
您可以看到$q

您可以在同一控制器中进行
$http
调用

$http
服务是一个函数,它接受一个参数(一个配置对象),用于生成http请求并返回一个带有两个$http特定方法的
承诺:
成功和错误
。它在内部使用
$q
(受Kris Kowal的q启发的承诺/延迟实现)

如果两个
$http
彼此独立,则使用
$q.all
来“加入”http调用的结果

例如:

$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]

  });
}
如果http调用相互依赖,则可以使用
链接的概念

例如:

$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;
    });
});
有关
q
的参考,您可以看到


有关
$q service
的参考信息,您可以看到$q

的可能副本,该副本不是创建另一个控制器所必需的,但我们也不知道您的应用程序的范围。可能的副本不是创建另一个控制器所必需的,但我们也不知道您的应用程序的范围。甚至不清楚这些呼叫是否与甚至不清楚这些调用是否相互关联,甚至需要链接