Francis Lim
Francis Lim

Reputation: 101

How to trigger function every http request in angularjs

I just wanna know if we can add a function in every http request? ex.

$http.post('/Customer/FecthCustomer');

I mean all throughout the controller without putting the function each and every request there is.

I want to solve a problem where I cant get the page loaded event after the data from the server was fetch. PS(window.setTimeout, window.load, $(document).ready,angular.element(document)) TESTED and DID NOT work, they only trigger every single load of the page.

Upvotes: 1

Views: 416

Answers (1)

Francis Lim
Francis Lim

Reputation: 101

Thanks to @holydragon!

Searched on interceptors.

Created this snippet:

angular.module('myModule', []).config(function($httpProvider) { 
    $httpProvider.interceptors.push(function($rootScope) {
        return {
            'request': function (config) {
                console.log('REQUEST TRIGGERED');
                return config;
            }
        }
    });
})

Upvotes: 1

Related Questions