I am utilizing 2 $http
functions within my AngularJS application to populate the page content. These functions are called using ng-init
.
ng-init = "getOpen(); getClosed();"
Is this the most optimal approach?
The first function is as follows:
$scope.getOpen = function () {
$http({
method: 'post',
url: "http://www.example.co.uk/php/open-get.php",
data: $.param({ 'location' : $scope.l,
'time' : $scope.t,
'day' : $scope.d,
'type' : 'get_restopen' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).
success (function(data, status, headers, config){
if(data.success && !angular.isUndefined(data.data) ){
$scope.open = data.data;
} else {
$scope.open = [];
}
}).
error(function(data, status, headers, config) {
//$scope.messageFailure(data.message);
});
}
The second function is as follows:
$scope.getClosed = function () {
$http({
method: 'post',
url: "http://www.example.co.uk/php/closed-get.php",
data: $.param({ 'location' : $scope.l,
'time' : $scope.t,
'day' : $scope.d,
'type' : 'get_restopen' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).
success (function(data, status, headers, config){
if(data.success && !angular.isUndefined(data.data) ){
$scope.closed = data.data;
} else {
$scope.closed = [];
}
}).
error(function(data, status, headers, config) {
//$scope.messageFailure(data.message);
});
}
My AngularJS implementation seems to be functioning well. However, I would like some insights on the efficiency of my current method.
1 - Are my $http
requests executed concurrently or does one complete before the other one starts?
2 - Would it be beneficial to incorporate $q
or promises
in my code? Considering that these functions operate independently.
3 - How can I determine when all $http
requests have finished execution, regardless of their outcome?
Am I correct in assuming the following code snippet accomplishes this?
$q.all([$scope.getOpen, $scope.getClosed]).then(function() {
//Both requests have been completed and I shall now set a boolean
$scope.complete = true;
});