Javascript 将多个$http合并为一个以解决范围问题

Javascript 将多个$http合并为一个以解决范围问题,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,我有以下代码的倍数;我怎样才能把它们结合起来呢?因为$scope.mydata甚至全局var mydata在$http之外是不可访问的 $http({ url: "php/mainLoad.php", method: "GET", params: {"userId":"1"} }).success(function(data, status, headers, config) { $scope.mydata = data;

我有以下代码的倍数;我怎样才能把它们结合起来呢?因为$scope.mydata甚至全局var mydata在$http之外是不可访问的

   $http({
    url: "php/mainLoad.php",
    method: "GET",
    params: {"userId":"1"}
    }).success(function(data, status, headers, config) {

        $scope.mydata = data;
        mydata = data;
    }).error(function(data, status, headers, config) {
       // $scope.status = status;
       alert(status);
    });

使用承诺$http是一种承诺,就像$q对象一样。首先注入$q,然后执行以下操作:

call1 = $http ({});
call2 = $http ({});
call3 = $http ({});

$q.all ([call1, call2, call3])
.then(function(response) {
    responseFromFirstCall = response[0];
    responseFromSecondCall = response[1];
    responseFromThirdCall = response[2];
}, function(errResponse) {
    firstErrorResponse = errResponse;
});

函数在。然后函数。。。只有在所有呼叫和承诺都已解决时才会触发。如果任何调用被拒绝,第二个函数将调用,errResponse将是第一个被拒绝调用的响应。第二个参数,即第二个函数,是可选的。

组合什么?你能澄清你的问题吗。还有更多的代码将有帮助。@smk如何使用$http进行多个加载?在开始时很难捕获它,但它非常方便。。。您可以在此处阅读$q:。正如我前面所说,$http也是$q的一个实例,它还具有一些其他功能,例如.success和.error。