【发布时间】:2015-06-27 00:50:53
【问题描述】:
我想在应用程序中编写错误处理部分, 我怎样才能删除这几秒钟并直接转到错误页面而不加载释放错误的主页?有没有办法在执行其控制器后加载 html 模板?
var interceptor = ['$rootScope', '$q', function (scope, $q) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 500) {
window.location= "http://www.domain.lan/#!/error";
return;
}
if (status == 403) {
// window.location = "dashboard";
return;
}
// otherwise
return $q.reject(responseInterceptors);
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
【问题讨论】:
-
是使用 $injector.get('$state').go('error');但对这个问题没有任何影响
-
为什么您不在 $routeProvider / $stateProvider 本身中处理此类错误。
-
我该怎么做?我是 angularjs 的新手
-
取决于返回请求错误的时间,在返回之前您无法猜测错误,如果您在加载或加载模板时运行 ajax 请求,您将无法在此之前重定向:)跨度>
-
因此,如果您想在模板之前重定向,您需要运行 $http 调用,例如在 routeProvider 解析函数中
标签: ajax angularjs error-handling exception-handling angularjs-routing