I'm working on angularJs and typescript project. I have to make synchronous http call and get some data from server before I launch my client app and load UI. I search on internet and see everybody speak about promise, huumm okay why not. So I use promise (make $http call and use $q to return promise) in my app.run(). Maybe I'm missing nothing because this is not working at all. Angular launch app.config(), then app.run(), ... But Angular does not wait app.config() finish before launch app.run(). So my first promise is launch in app.run() and before it resolve Angular try to instantiate controller... I don't want to create new service call httpSynchronous but I haven't any other ideas.
Asked
Active
Viewed 1.1k times
8
-
2Update with some code please :D – Seth Feb 19 '15 at 14:48
-
Can you move the http synchronous call to your app.run? Then you can use your code which depends on the result into the success block. – Or Guz Feb 19 '15 at 14:48
1 Answers
17
Angular does not support asynchronous actions in .config
and .run
functions. If you want to delay your application, there are 2 ways:
- The first one is to delay your whole application by using angular.bootstrap() to manually start it. But anything you do is outside angular, so you don't have access to anything but vanilla JavaScript.
- The second one is to use the resolve property of your routes. If you use a router like angular route segment or ui router, you can define a top route / state with a resolve which would be resolved when your application loads (and if you force a full reload of your route).

Thomas Roch
- 1,208
- 8
- 19
-
Thanks for your answer but i cannot use first solution, cause i have to make mutiple http call and initialize angularJs component (service, directive,...) so this solution will be very ugly. And I already see this solution on internet but put all this logic in my route's definition is not the right way. Resolve is use to resolve services for controller and this is not my goal. – Felix_Billon Feb 19 '15 at 15:19
-
-
2
-
An issue with the `resolve` approach, is that if you call `$state.reload` at any point in your controller, the resolve function will execute again. So if you're loading once-off data when your app starts, but then reloading the state because you want to update the page, then you'll end up reloading the once-off data... – Sean Feb 03 '18 at 18:36
-
When you call `$state.reload`, you can specify the hierarchy of the state you are reloading. The parent `resolves` are not called then. – berturion Oct 27 '21 at 12:25