Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does angularjs have something similar to jQuery's ajaxSetup?

Tags:

angularjs

I've browsed through the documentation but couldn't find anything mentioning this. I'm using Angular's $http service and want to run code before every ajax event, and after every ajax event (to display a loading message while waiting for the server to respond).

I have jQuery included and tried doing:

$(document).ready(function() {
    $('body').ajaxSend(function(event, jqXHR) {
        console.log('in jquery send');
    }) ;
});

But couldn't get the message logged to the console. Any help is appreciated.

like image 666
Brian DiCasa Avatar asked Dec 05 '12 22:12

Brian DiCasa


1 Answers

Please check out the docs on $http service: http://docs.angularjs.org/api/ng.$http

You will see that there are two options that you can use:

  1. httpInterceptor (see Response interceptors section in the docs) - it allows you to wrap every response and do whatever you want before it completes the request. It uses promises so it's really easy to even do some async stuff after each request.

  2. request/response transformations (see Transforming Requests and Responses) - it allows you to process every request and response that goes through $http - so that would be my choice as it allows you to hook before and after the request.

like image 96
matys84pl Avatar answered Oct 29 '22 17:10

matys84pl