0

I create an app with requirement of template files being in two folders - first is available without restrictions and second is available only for logged in users.

Things are complicating when somebody refresh the page and session expire before. Angular throws error (e.g. 401) in console while loading template file which I want to avoid. Is there a way to catch that event and access response status to for e.g. redirect the page?

4
  • so you want to redirect the page whenever there is an error? Commented Sep 9, 2015 at 14:58
  • Perfectly redirect or change location.hash will be one of the options, depends of the response status. Commented Sep 9, 2015 at 14:59
  • 1
    Have you thought about $http interceptors? you can define what to do whenever errors happen then redirect to an err page? docs.angularjs.org/api/ng/service/$http#interceptors Commented Sep 9, 2015 at 15:10
  • @MicheleRicciardi - thank you, it is what I looked for. Commented Sep 9, 2015 at 15:22

1 Answer 1

1

For your case, we can catch the error and redirect to a page with the help of $location.url(). Here is the concept. Now i have a controller and in that, i have success function and error function. Whenever we get an error, the error function will run and we can pass the link of the page you want to redirect.

$http.get(url,[params])
     .success(function(data, status, headers, config){
    // bind your data to scope
      })
     .error(function(data, status, headers, config) {
        $location.url('/404');
      });

By the use of $routeProvider, you can configure the function something like this :

$routeProvider
   .when('/404', {
       templateUrl: '404.html',
       controller: 'yourcontroller'
});

And you can see the description for $location.url() here

Hope it works

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.