I have an angularjs project. I am just wondering, how to show the user a message, if a requested view/partial is not found (HTTP 404). At the moment, angular starts the request and gets a 404 response including the error-html, but the user doesn't see any change to the website.
1 Answer
Add an $http interceptor (scroll down on this page) for 'responseError'
angular.module("app").config(function($provide, $httpProvider) {
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
'responseError': function(response) {
if (response.status == 404) {
// user hit a 404 -- you can check response.url to see if it matches
// your template directory and act accordingly
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('myHttpInterceptor');
});
3 Comments
bernhardh
Response interceptors are deprecated :/. And I am using $ressource not $http.
a better oliver
@leftjustified Mind the difference between
$httpProvider.responseInterceptors and $httpProvider.interceptors. $resource uses $http and in your question you talk about views. I see no connection to $resource.bernhardh
Oh. Ok, sorry, my fault. I found a great blogpost on this topic: blog.brunoscopelliti.com/http-response-interceptors