This should be easy but I cant seem to get it to work.
I have the below code which are basically fetching data using $http.
FYI I'm using POST and not GET.
Right now they run in parallel. One might finish before the other. My goal is to present the data once all have finished. So I read up on $q but I cant seem to get it to work.
$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 = [];
});
}
As you can see I can get the returned data from my $http calls in the main function itself; $scope.closed = response.data.data; and $scope.open = response.data.data;
But I don't want to assign them to the $scope just yet until both have completed. So I should be able to use $q to do the below but I don't get data in my $scope and no errors.
$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
});
Am I doing something wrong?
$q