Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ui-router otherwise to state instead of url?

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.

like image 880
Cameron Avatar asked Oct 03 '14 14:10

Cameron


3 Answers

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.

like image 141
tucq88 Avatar answered Sep 19 '22 20:09

tucq88


I think you achieved that with this code

$urlRouterProvider.otherwise(function($injector, $location){
  $injector.invoke(['$state', function($state) {
    $state.go('404');
  }]);
}); 
like image 24
kdlcruz Avatar answered Nov 13 '22 22:11

kdlcruz


Try This.

$urlRouterProvider.otherwise(function($injector, $location){
    $injector.get('$state').go('404');
});
like image 3
danijelf Avatar answered Nov 13 '22 22:11

danijelf