3

We have a couple of states for one page, so the states don't have any URL assigned to them. Is there a way to specify a default state for URL-less states? Maybe similar to $urlRouterProvider.otherwise('/');.

I tried going to a state in Angular's run statement or within the controller but that causes a transition superseded error, which is probably due to the fact that $state hasn't been initialized.

Below is a short example. I would like to start directly in stateA.

angular
  .module('myModule')
  .config([
    '$stateProvider',
    function($stateProvider) {
      $stateProvider
        .state('stateA', {
          controller: function () {
            // Do some magic related to state A
          }
        })
        .state('stateB', {
          controller: function () {
            // Do some magic related to state B
          }
        });
    }
  ])
  .controller(['$state', '$timeout', function($state, $timeout){
    // My global controller
    // To set a default state I could do:
    // $timout(function () {
    //   $state.go('stateA'); 
    // }, 0);
    // But that doesn't feel right to me.
  }]);

Update: Thanks to this answer I figured out that I have to wrap $state.go into a $timeout, to avoid the transition superseded error.

4
  • Isn't that what $urlRouterProvider.otherwise does? Commented Jun 29, 2015 at 20:44
  • I thought its for specifying a default URL. Hence its name. Commented Jun 29, 2015 at 20:45
  • It should take your app to that state (/ in this case) if no route matches Commented Jun 29, 2015 at 20:47
  • 1
    I too have this question. I am working in an embedded application that cannot use, modify, or rely on the URL in any way but I want a default state. $urlRouterProvider.otherwise doesn't seem right for this use-case Commented Jul 14, 2015 at 17:27

1 Answer 1

5

This will call a custom rule that goes to a default state without tampering with a URL.

$urlRouterProvider.otherwise(function($injector) {
    var $state = $injector.get('$state');
    $state.go('stateA');
});`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.