3

Using UIRouter in an Angular 1.5 app, I'm having trouble trying to set an initial state for my angular module.

I've created a few states without URLs (because I don't need actual routing) and I would like to activate an initial state once the module is instantiated.

Usually, I would do this using $urlRouterProvider.otherwise(<initialRoute>), but since I don't use URLs in my states, that can't work.

Does UIRouter (or Angular) provide a solution for this?

4
  • sorry but if you don't have route ..why di you want to set one? .. have you tried already $urlRouterProvider.otherwise('/') and set <base ref="/"> in your index? Commented Jan 18, 2017 at 16:41
  • No I don't have any route, juste states without URLs. It's actually a feature of UIRouter, you can activate such a state the same way as for a routed state (using ui-sref or $state.go() for example). Similarly to a routed application where you usually define a default URL (such as /home for example), I would like to have a default state (that is not URL-based). Is that clearer? Commented Jan 18, 2017 at 16:48
  • but ui-sref and $state.go are used to navigate to a url .. but ok Commented Jan 18, 2017 at 16:50
  • I know, it looks weird but it is intended to work like that :) Commented Jan 18, 2017 at 17:14

1 Answer 1

6

After your application has bootstrapped, you can set your initial state inside the .run() method.

angular.module("myModuleName")
    .run([
        "$state",
         function($state){
            $state.go('stateName');
         }
     ]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works well! Indeed, it makes perfect sense and that's a clean solution :)

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.