最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to manage 404 errors loading directive templates in AngularJS - Stack Overflow

programmeradmin3浏览0评论

In an AngularJS directive the templateUrl parameter is defined dinamically.

'templates/' + content_id + '.html'

I don't want to establish rules to check if content_id value is valid and manage it as 404 errors, i.e. if the template doesn't exist (server return a 404 error when loading the template) load template/404.html instead.

How can I do that?

Edited: The current answers suggest to use a response error interceptor. In this case ¿how can I know that the response is to a loading of this template?

In an AngularJS directive the templateUrl parameter is defined dinamically.

'templates/' + content_id + '.html'

I don't want to establish rules to check if content_id value is valid and manage it as 404 errors, i.e. if the template doesn't exist (server return a 404 error when loading the template) load template/404.html instead.

How can I do that?

Edited: The current answers suggest to use a response error interceptor. In this case ¿how can I know that the response is to a loading of this template?

Share edited Jun 23, 2015 at 20:56 francadaval asked Jun 23, 2015 at 10:05 francadavalfrancadaval 2,4813 gold badges26 silver badges37 bronze badges 2
  • I suspect this would be better handled on the server-side. – Brandon Commented Jun 23, 2015 at 10:52
  • Maybe, but I want to know how to do it. – francadaval Commented Jun 23, 2015 at 11:20
Add a ment  | 

2 Answers 2

Reset to default 6

You will need to write response error interceptor. Something like this:

app.factory('template404Interceptor', function($injector) {
    return {
        responseError: function(response) {
            if (response.status === 404 && /\.html$/.test(response.config.url)) {
                response.config.url = '404.html';
                return $injector.get('$http')(response.config);
            }
            return $q.reject(response);
        }
    };
});

app.config(function($httpProvider) {
    $httpProvider.interceptors.push('template404Interceptor');
});

Demo: http://plnkr.co/edit/uCpnT5n0PkWO53PVQmvR?p=preview

You can create an interceptor to monitor all requests made with the $http service and intercept any response errors. If you get a status 404 for any request made, simply redirect the user to error page(template/404.html in your case).

.factory('httpRequestInterceptor', function ($q) {
    return {
        'responseError': function(rejection) {
            if(rejection.status === 404){
                // do something on error                   
             }

            }
            return $q.reject(rejection);
         }
     };
});

You would need to push the interceptor to $httpProvider in your config function.

myApp.config( function ($httpProvider, $interpolateProvider, $routeProvider) {
    $httpProvider.interceptors.push('httpRequestInterceptor');
});

Here's the demo

Cheers!

发布评论

评论列表(0)

  1. 暂无评论