AngularJS ui-router default state for states without a URL

F Lekschas

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.

CrIceIs

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');
});`

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

AngularJS ui router passing data between states without URL

From Java

AngularJS UI Router - change url without reloading state

From Dev

angularjs ui-router: Get list of child states of a given state

From Dev

AngularJS UI-Router : Abstract state + child states not working

From Dev

Walking by nested states in ui-router without reloading parent state

From Dev

AngularJS UI-Router not getting to default state

From Dev

Set Default View for multiple states AngularJS ui.router

From Dev

Parameters for states without URLs in ui-router for AngularJS

From Dev

[AngularJS][ui-router]: optional params in states in the middle of URL

From Dev

AngularJS / UI Router - Locale in State URL / TemplateURL

From Dev

Removing default ui-router state then nested state is active in AngularJS

From Dev

angularjs ui-router default child state and ui-sref

From Java

Redirect a state to default substate with UI-Router in AngularJS

From Dev

AngularJS: How to have default nested state using UI Router?

From Dev

How to specify a default state in AngularJS ui-router module

From Dev

In AngularJS ui-router, state changed, url changed but template url not

From Dev

In AngularJS ui-router, state changed, url changed but template url not

From Dev

Angular UI-router default state on same URL

From Dev

How to get Angularjs UI Router list of states?

From Dev

angularjs - ui.router sibling states

From Dev

AngularJS, ui-router, nested states

From Dev

AngularJS UI Router shared nested routes/states

From Dev

angularjs - ui.router sibling states

From Dev

Angularjs ui.router chid states not loading

From Dev

Parsing UI router states to URLs without Angular

From Dev

AngularJS ui-router otherwise to state instead of url?

From Dev

AngularJS UI-Router : Get the absolute URL of the state with the parameters

From Dev

stateParams in url of abstract state, ui-router, angularjs

From Dev

angularjs ui-router: redirect vs multiple url for same state

Related Related

  1. 1

    AngularJS ui router passing data between states without URL

  2. 2

    AngularJS UI Router - change url without reloading state

  3. 3

    angularjs ui-router: Get list of child states of a given state

  4. 4

    AngularJS UI-Router : Abstract state + child states not working

  5. 5

    Walking by nested states in ui-router without reloading parent state

  6. 6

    AngularJS UI-Router not getting to default state

  7. 7

    Set Default View for multiple states AngularJS ui.router

  8. 8

    Parameters for states without URLs in ui-router for AngularJS

  9. 9

    [AngularJS][ui-router]: optional params in states in the middle of URL

  10. 10

    AngularJS / UI Router - Locale in State URL / TemplateURL

  11. 11

    Removing default ui-router state then nested state is active in AngularJS

  12. 12

    angularjs ui-router default child state and ui-sref

  13. 13

    Redirect a state to default substate with UI-Router in AngularJS

  14. 14

    AngularJS: How to have default nested state using UI Router?

  15. 15

    How to specify a default state in AngularJS ui-router module

  16. 16

    In AngularJS ui-router, state changed, url changed but template url not

  17. 17

    In AngularJS ui-router, state changed, url changed but template url not

  18. 18

    Angular UI-router default state on same URL

  19. 19

    How to get Angularjs UI Router list of states?

  20. 20

    angularjs - ui.router sibling states

  21. 21

    AngularJS, ui-router, nested states

  22. 22

    AngularJS UI Router shared nested routes/states

  23. 23

    angularjs - ui.router sibling states

  24. 24

    Angularjs ui.router chid states not loading

  25. 25

    Parsing UI router states to URLs without Angular

  26. 26

    AngularJS ui-router otherwise to state instead of url?

  27. 27

    AngularJS UI-Router : Get the absolute URL of the state with the parameters

  28. 28

    stateParams in url of abstract state, ui-router, angularjs

  29. 29

    angularjs ui-router: redirect vs multiple url for same state

HotTag

Archive