问题描述:

In angularjs is it possible to handle & resolve 404 Views?

I want to achieve conditional view rendering depending on my client, I have a view something like this:

<div ng-include="'assets/stackoverflow/breadcrumb.html'"></div>

so if suppose the above returns 404, it should try this:

<div ng-include="'assets/default/breadcrumb.html'"></div>

This approach may be wrong, but I just need a way to handle views 404, I have an interceptor which actually catches 404 requests but how do I resolve it and send it again?

app.factory('RequestResponseInterceptor', ['$q', '$rootScope', '$location',

function($q, $rootScope, $location) {

var loadingCount = 0;

return {

responseError: function(response) {

if (response.status === 404) {

//Change URL and request again

}

return $q.reject(response);

}

};

}

]);

网友答案:

You should use routing.

Configure your app like

.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
    templateUrl : 'assets/stackoverflow/breadcrumb.html',
    controller : 'ctrl1'
}).when('/s404', {
    templateUrl : 'assets/default/breadcrumb.html',
    controller : 'ctrl2'
}).otherwise({
    redirectTo : '/'
});

Replace ng-include with ng-view:

<ng-view></ng-view>

Set the location path in interceptor (and remember to inject $location) :

if (response.status === 404) {
   $location.path( "/s404" );
}
相关阅读:
Top