I'm making a single page application (SPA). I made a controller called InitialControler
to load the data from the server at this url (local.app/init).
I want this url to be opened before any other url. I'm using ui-router, I did a $state.go('init')
in the .run()
function but it still load the requested page before the 'init'
page
If you are using ui-router then you could resolve this using nested states. For example:
In this example you have defined 3 routes: "/", "/landing", "/profile"
So, InitController (related to "/" route) gets called always, even if the user enters directly at /landing or /profile
Important: Don't forget to include
<div ui-view></div>
to enable the child states controller load on this sectionOne way to do is, in config declare only 'init' state. And in InitialController, after data is loaded(resolve function of service call), configure other states. But in this approach, whenever you refresh the page, the url will change to local.app.init.
To stay in that particular state even after reloading, the solution I found is to have a StartUp app in which I loaded the required data and after that I bootstraped the main app manually by angular.bootstrap.
First create state called app
Now, any new state you create should be child state of
app
state. This is also good because it become sort of your root scope. And state will not process unless your factory resolves.This is how you create your factory
Now in any other state
More on sate resolve
https://github.com/angular-ui/ui-router/wiki#resolve