AngularJS ui-router otherwise to state instead of url?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 07:48:08

问题


I am using AngularJS and ui-router and in my app.js. I have the following line:

$urlRouterProvider.otherwise('/');

However I do not want it to send the user to a URL but instead to a state:

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

Normally I would just do this:

$state.go('404');

How can I do this for the otherwise method?

Note: that my 404 state does not have a URL, so basically it keeps the URL that the user has entered or visited and just changes the template.


回答1:


I think you achieved that with this code

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



回答2:


Try This.

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



回答3:


Just a small improvement to @kdlcruz's answer:

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

By doing that, you still able to keep the incorrect the URL and only state is changed.



来源:https://stackoverflow.com/questions/26181141/angularjs-ui-router-otherwise-to-state-instead-of-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!