Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am familiar with Jquery AJAX call, which has different callbacks like beforeSend, success, complete, etc.

This is the example AJAX call with Jquery:

$.ajax({
  url: 'register.php',
  type: 'POST',
  data: {name:name, email:email},
  beforeSend: function() {
       // show loading GIF
  },
  complete: function() {
      // hide loading GIF
  },
  success: function(data) {
      // parse response
  }
});

I want to achieve the same using AngularJS.

Is there a callback like beforeSend for AngularJS AJAX request ? This is my code so far, but i am not sure where can i use a callback like beforeSend (so that i can display a loading GIF image) in my code:

$http.post('register.php', {'name': $scope.name, 'email': $scope.email})
.success(function(data, status, headers, config) {
    if (data != '') { 
    }
});
share|improve this question

1 Answer 1

You can use interceptors. Search for the word interceptor in the Angular $http documentation

As the documentation says : For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses

Here is a Fiddle Example displaying a loading gif before the ajax call is sent.

EDIT

As Satpal commented, $httpProvider.responseInterceptors used in the Fiddle is deprecated. You should use $httpProvider.interceptors instead.

I don't remove the Fiddle as it can give you an idea about how to implement it.

share|improve this answer
1  
$httpProvider.responseInterceptors are DEPRECATED. You should provide example with $httpProvider.interceptors –  Satpal Mar 3 '14 at 7:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.