0

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 1

1

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');

});
Sign up to request clarification or add additional context in comments.

3 Comments

Response interceptors are deprecated :/. And I am using $ressource not $http.
@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.
Oh. Ok, sorry, my fault. I found a great blogpost on this topic: blog.brunoscopelliti.com/http-response-interceptors

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.