Search code examples
angularjsangular-http-interceptors

Is it possible to intercept html, css and js files used in angularjs?


I have already created an http interceptor and based on my observation, it only applies to all that uses $http service. What if I want to intercept even the css, html and js files used in the template? Is it possible?

This is the code of the interceptor I mentioned above:

app.factory('redirectInterceptor', function($q,$location,$window){
return  {
    'response':function(response){
    if (typeof response.data === 'string' && response.data.indexOf("My Login Page")>-1) {
        $window.location.href = "/login.html";
        return $q.reject(response);
    }else{
        return response;
    }
    }
}

});

app.config(['$httpProvider',function($httpProvider) {
    $httpProvider.interceptors.push('redirectInterceptor');
}]);

I want to be able to do an interceptor for css, html and js (for template) just like the one above.


Solution

  • As you said,

    it only applies to all that uses $http service

    So, if you want to intercept the requests for html,css and js files. It is best done on the server, rather than the client.