gpt4 book ai didi

javascript - 使用 AngularJS 路由器加载 404 状态

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:10 24 4
gpt4 key购买 nike

我正在使用 AngularJSui-router并使用以下代码捕获 404 并在 404 模板中加载:

$urlRouterProvider.otherwise(function($injector, $location){
$injector.invoke(['$state', function($state) {
$state.go('404');
}]);
});

它保持 URL 不变而不是重定向,但显示 404 模板:

.state('404',
{
views: {
'body': {
templateUrl: 'partials/404.html',
}
}
});

通常它会重定向到根状态:$urlRouterProvider.otherwise('/');

然而,当 HTML5 模式关闭并且用户访问根 URL 时,例如http://www.domain.com/app/ 它加载 404 状态而不是重定向到 http://www.domain.com/app/#/

如何保留上面的 404 状态代码,但在访问初始页面时让它重定向到 hashbang?如果请求不是主页,那么实际上只有 404 加载?

我不能使用 $stateNotFound$stateChangeSuccess 并且需要一种方法来检查它是否是实际配置设置本身中可以在两者之间切换的主页请求otherwise 语句中的 /state.404

最佳答案

所以类似的东西(这似乎确实有效)。

$urlRouterProvider.otherwise(function($injector, $location){
$injector.invoke(['$state', function($state) {
if( $location.$$path == '')
$state.go('home'); // redirect to /#/
else
$state.go('404'); // else go to 404 state
}]);
});

但是有更好的方法来处理这个问题吗?

关于javascript - 使用 AngularJS 路由器加载 404 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36202780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com