Javascript AngularJS ui路由器是否以状态而不是url?
我在我的Javascript AngularJS ui路由器是否以状态而不是url?,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我在我的app.js中使用and。我有以下几行: $urlRouterProvider.otherwise('/'); 但是我不希望它将用户发送到URL,而是将发送到一个状态: .state('404', { views: { 'body': { templateUrl: 'partials/404.html', } }
app.js
中使用and。我有以下几行:
$urlRouterProvider.otherwise('/');
但是我不希望它将用户发送到URL,而是将发送到一个状态:
.state('404',
{
views: {
'body': {
templateUrl: 'partials/404.html',
}
}
});
通常我会这样做:
$state.go('404');
对于其他方法,如何执行此操作
注意:我的404状态没有URL,因此它基本上保留了用户输入或访问的URL,只是更改了模板。我认为您通过这段代码实现了这一点
$urlRouterProvider.otherwise(function($injector, $location){
$injector.invoke(['$state', function($state) {
$state.go('404');
}]);
});
试试这个
$urlRouterProvider.otherwise(function($injector, $location){
$injector.get('$state').go('404');
});
只是@kdlcruz答案的一个小改进:
$urlRouterProvider.otherwise(function($injector){
$injector.invoke(['$state', function($state) {
$state.go('404', {}, { location: false } );
}]);
});
通过这样做,您仍然能够保留错误的URL,并且只更改状态