【发布时间】:2014-10-11 07:57:24
【问题描述】:
在我的 AngularJS 应用程序中,我想捕获错误并将用户发送到 404 状态,如下所示:
$rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams){
//console.log(unfoundState.to);
//console.log(unfoundState.toParams);
//console.log(unfoundState.options);
$state.go('404');
});
这适用于当用户点击应用程序中的错误链接(例如没有状态,有状态但没有内容的链接由 else 方法处理)等时。
代码加载404状态正常:
.state('404',
{
views: {
'body': {
templateUrl: 'partials/404.html',
}
}
});
但我想做的是将 URL 更改为他们尝试访问的状态,因此基本上加载 404 状态但使用不正确的 URL(因为 404 将在服务器环境中工作)。
我正在考虑这样做:
history.replaceState(null, null, unfoundState.to);
$state.go('404');
但这会导致应用出现重大错误并更改 URL 而不是状态!
我该怎么做?
【问题讨论】:
-
我没试过。我不确定
$state.transitionTo('404',{},{location:false});是否有效。
标签: javascript angularjs angular-ui-router