1

I've the following module:

var mod;

mod = angular.module('ajax-interceptor', []);

mod.config(function($httpProvider) {

    $httpProvider.interceptors.push(["$q", function($q, dependency1, dependency2) {
        return {
            'request': function(config) {
                //want to update a scope variable when a request is placed
                return config;
            },

            'requestError': function(rejection) {
                return $q.reject(rejection);
            },

            'response': function(response) {
                return response;
            },

            'responseError': function(rejection) {
                return $q.reject(rejection);
            }
        };
    }]);
});

I want to to update a scope variable whenever a request is placed. How can I do that? I can't inject $scope in the config block. I'll use this variable to keep track the activity of the user, like a user not requesting for 10 min the user will be forcefully logged out.

1 Answer 1

1

You can add a factory to your module and keep track of user activities in it:

mod.factory("factoryName", [
    function(){
        var factory = {};
        factory.track = function(userActivity){
            // Save user activity
        };

        return factory;
    }
]);

Then you must inject the factory into $httpProvider and use it:

$httpProvider.interceptors.push(["$q", "factoryName", function($q, factoryName, dependency1, dependency2) {
    return {
        'request': function(config) {
            //want to update a scope variable when a request is placed
            factoryName.track(userActivity);
            return config;
        }
    };
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.