在我的 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');
});
这适用于用户点击应用程序中的不良链接(例如没有状态,因为有状态但没有内容的链接由其他方法处理)等。
代码加载 404 状态正常:
.state('404',
{
views: {
'body': {
templateUrl: 'partials/404.html',
}
}
});
但我想要做的是将 URL 更改为他们尝试访问的状态,因此基本上加载 404 状态但使用不正确的 URL(因为 404 会在服务器环境中工作)。
我考虑过这样做:
history.replaceState(null, null, unfoundState.to);
$state.go('404');
但这会导致应用程序出现重大错误并更改 URL,但不会更改状态!
我该怎么做?
最佳答案
try catch 与您之前的任何路由不匹配的所有 URL:
.state("404", {
url: "*path", // will catch all URLs
templateUrl: 'partials/404.html'
})
关于javascript - AngularJS ui-router 到一个状态但使用不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26181771/