Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
    
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? – federico scamuzzi Jan 18 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? – ClementParis016 Jan 18 at 16:48
    
but ui-sref and $state.go are used to navigate to a url .. but ok – federico scamuzzi Jan 18 at 16:50
    
I know, it looks weird but it is intended to work like that :) – ClementParis016 Jan 18 at 17:14
up vote 3 down vote accepted

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');
         }
     ]);
share|improve this answer
    
Thank you, it works well! Indeed, it makes perfect sense and that's a clean solution :) – ClementParis016 Jan 18 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.