Cam*_*ron 6 javascript angularjs angular-ui-router
我正在使用AngularJS和ui-router,并使用以下代码捕获404并在404模板中加载:
$urlRouterProvider.otherwise(function($injector, $location){
$injector.invoke(['$state', function($state) {
$state.go('404');
}]);
});
Run Code Online (Sandbox Code Playgroud)
它保持URL完整而不是重定向,但显示404模板:
.state('404',
{
views: {
'body': {
templateUrl: 'partials/404.html',
}
}
});
Run Code Online (Sandbox Code Playgroud)
通常它会重定向到根状态: $urlRouterProvider.otherwise('/');
但是,当关闭HTML5模式并且用户访问根URL时,例如http://www.domain.com/app/它加载404状态而不是重定向到http://www.domain.com/app/#/
如何保持上面的404状态代码,但在访问初始页面时将其重定向到hashbang?如果请求不是主页,那么只有404加载才有效吗?
我不能使用$stateNotFound或$stateChangeSuccess并需要一种方法来检查它是否是实际的配置设置本身可以在切换中的主页请求/和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
}]);
});
Run Code Online (Sandbox Code Playgroud)
但有更好的方法来处理这个问题吗?