Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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

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 使用$q.all()从AngularJS中的函数获取$http数据_Javascript_Angularjs - Fatal编程技术网

Javascript 使用$q.all()从AngularJS中的函数获取$http数据

Javascript 使用$q.all()从AngularJS中的函数获取$http数据,javascript,angularjs,Javascript,Angularjs,这应该很容易,但我似乎无法让它工作。 我有下面的代码,基本上是使用$http获取数据 仅供参考,我使用的是POST,而不是GET 现在它们并行运行。一个可能比另一个先完成。我的目标是在所有工作完成后显示数据。所以我读了$q,但我似乎无法让它发挥作用 $scope.getRestOpen = function () { $http({ method: 'post', url: "http://www.xxx.co.uk/php/xxx/restope

这应该很容易,但我似乎无法让它工作。 我有下面的代码,基本上是使用
$http
获取数据

仅供参考,我使用的是POST,而不是GET

现在它们并行运行。一个可能比另一个先完成。我的目标是在所有工作完成后显示数据。所以我读了
$q
,但我似乎无法让它发挥作用

    $scope.getRestOpen = function () {
    $http({
        method: 'post',
        url: "http://www.xxx.co.uk/php/xxx/restopen-get.php",
        data: $.param({ 'location' : $scope.l, 
                       'time' : $scope.t,
                       'day' : $scope.d,
                       'type' : 'get_restopen' }),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        $scope.open = response.data.data;
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        $scope.open = [];
    });
}

$scope.getRestClosed = function () {
    $http({
        method: 'post',
        url: "http://www.xxx.co.uk/php/xxx/restclosed-get.php",
        data: $.param({ 'location' : $scope.l, 
                       'time' : $scope.t,
                       'day' : $scope.d,
                       'type' : 'get_restclosed' }),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        $scope.closed = response.data.data;
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        $scope.closed = [];
    });
}
如您所见,我可以从主函数本身的
$http
调用中获取返回的数据<代码>$scope.closed=response.data.data和
$scope.open=response.data.data

但我不想将它们分配给
$scope
,直到两者都完成。因此,我应该能够使用
$q
执行以下操作,但我没有在
$scope
中获取数据,并且没有错误

$q.all([$scope.getRestOpen, $scope.getRestClosed]).then(function(data){
    $scope.open = data[0].data; // doesn't work
    $scope.closed = data[1].data // doesn't work
});

我做错什么了吗?

您需要让
$q.all()
数组中的每一项都返回一个承诺。因为没有返回任何内容,所以您的响应将是
[未定义,未定义]

您只需替换
$scope.open=response.data.data带有
返回response.data.data并且它应该可以工作。确保在挡块中执行相同的操作

编辑:下面是代码的外观

$scope.getRestOpen = function () {
    return $http({
        method: 'post',
        url: "http://www.xxx.co.uk/php/xxx/restopen-get.php",
        data: $.param({ 'location' : $scope.l,
                'time' : $scope.t,
                'day' : $scope.d,
                'type' : 'get_restopen' }),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        return response.data.data;
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        return [];
    });
}

$scope.getRestClosed = function () {
    return $http({
        method: 'post',
        url: "http://www.xxx.co.uk/php/xxx/restclosed-get.php",
        data: $.param({ 'location' : $scope.l,
                'time' : $scope.t,
                'day' : $scope.d,
                'type' : 'get_restclosed' }),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
        return response.data.data;
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        return [];
    });
}

像这样修改代码

 $scope.getRestOpen = function () {
    return  $http({
       method: 'post',
       url: "http://www.xxx.co.uk/php/xxx/restopen-get.php",
       data: $.param({ 'location' : $scope.l, 
                   'time' : $scope.t,
                   'day' : $scope.d,
                   'type' : 'get_restopen' }),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    });
 }

 $scope.getRestClosed = function () {
    return $http({
      method: 'post',
      url: "http://www.xxx.co.uk/php/xxx/restclosed-get.php",
      data: $.param({ 'location' : $scope.l, 
                   'time' : $scope.t,
                   'day' : $scope.d,
                   'type' : 'get_restclosed' }),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  });
 }

 $q.all([$scope.getRestOpen(), $scope.getRestClosed()]).then(function(data){
   $scope.open = data[0].data; 
   $scope.closed = data[1].data;
 });

查看此示例并阅读代码中的注释:

var myApp=angular.module('myApp',[]);
函数MyCtrl($scope、$q、$timeout){
var thenFn=函数(值){
console.log('resolved',值);
返回值;
},
q1=$scope.q1=$q.defer(),
q2=$scope.q2=$q.defer(),
p1=$scope.q1.promise,
p2=范围为$scope.q2.promise;
//等待完成所有请求
$q.all([
p1.然后(然后fn),
p2.然后(然后fn)
])
.然后(函数(值){
$scope.fromThen=值;
});
//必须启动AngularJS摘要进程
//允许$q.resolve()正常工作
//因此,请使用$timeOut()或$apply()
setTimeout(函数(){
$scope.$apply(函数(){
log(“解决延迟承诺”);
问题1.决心({
价值:1
});
问题2.决心({
价值:2
});
});
},100,这个);
/* 
*替代方法
*
$timeout(函数(){
log(“解决延迟承诺”);
q1.解析({value:1});
q2.解析({value:2});
});
*/
}

{{fromThen}}

不知道如何更改我所有的代码以使其像那样工作。我的两个函数没有问题。只需要关于如何合并
$q
并将承诺返回给作用域值的指导请记住调用这两个函数:
$scope.getRestOpen()
。有没有可能修改我的代码来实现this@moh.ABK编辑了我的回复,以包括代码的外观