Sako73
Sako73

Reputation: 10137

Why does the request function of my AngularJS HTTP interceptor get called so many times?

I have an AngularJS HTTP interceptor:

$httpProvider.interceptors.push([
    "$rootScope", "$q", "$location",
    function ($rootScope, $q, $location) {

        return {
            'request': function (config) {
                var loc = $location.path();
                console.log("path: " + loc);
                ....
                ....
                return config;
            },
            ...
            };
        }
    ]);

I make a call that returns an array of 25 items. The items are populated into the html using ng-repeat. This display results in 75 calls to the request function.

Can anybody explain why it makes so many calls for one HTTP request?

Thanks for any help.

Upvotes: 1

Views: 167

Answers (1)

Martijn Welker
Martijn Welker

Reputation: 5605

The http interceptor gets called for every request angular does, not just data requests. So also template requests, this might explain why it's so high.


Update: to filter out the templates I always use the following function.

function isLocalUrl(url) {
    return !(url.indexOf('http://') === 0 || url.indexOf('https://') === 0);
}

and call it like this:

return {
    'request': function (config) {
        if(isLocalUrl(config.url))
            return config;
    }
}

Upvotes: 2

Related Questions