0

I need to load some parameters from Api end point before bootstrap application Angular 1.

I have a factory that contains my functions for request end point and save in memory. I need to use this factory to execute my roles. The problem is that I can not inject my facotry before bootstrap application. I can not create other roles that do the same thinks, i have to use this factory for my application working.

Anybody have a good suggestion about this case? Who already needed this case?

Thanks everyone.

  • Why are you loading parameters for the AngularJS app from an API first? – Ben Apr 04 '17 at 01:08
  • Because my application is a solution in Sharepoint 2013, I use OData from Api Lists, and it is in them that I find the settings that I use during the entire application – Railson Lima Apr 04 '17 at 11:18
  • Is there no way to know these values up front and hard code them into the app? – Ben Apr 04 '17 at 12:08
  • Not possible, these data are available in Sharepoint lists – Railson Lima Apr 04 '17 at 12:37
  • If it's data and not configuration values that you need you'll need for your application, then you need to use a factory like you've been doing and immediately resolve that factory data after bootstrapping the application. – Ben Apr 04 '17 at 13:13

2 Answers2

0

If you are using the ngRoute router, you can use resolve functions to delay instantiation of the view and its controller until data arrives from an asynchronous API.

  • resolve - {Object.<string, Function>=} - An optional map of dependencies which should be injected into the controller. If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. If all the promises are resolved successfully, the values of the resolved promises are injected and $routeChangeSuccess event is fired. If any of the promises are rejected the $routeChangeError event is fired. For easier access to the resolved dependencies from the template, the resolve map will be available on the scope of the route, under $resolve (by default) or a custom name specified by the resolveAs property (see below). This can be particularly useful, when working with components as route templates.

— AngularJS ngRoute $routeProvider API Reference

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Tks, I'll need apply the resolve into all my controllers in .config? Do you have some examples with this implementetion? Let's suppose that we have two controllers, both need to wait this promise, because the user will can change the url in his browser. – Railson Lima Apr 04 '17 at 22:08
  • See [*Route Resolve and Controller Activate in AngularJS* by John Papa](https://johnpapa.net/route-resolve-and-controller-activate-in-angularjs/) – georgeawg Apr 04 '17 at 23:53
-1

I think you should be ok using the angular-ui-router. You can use resolve on your routes. This will force Angular to resolve the data before moving on to rendering the controller. You can setup resolve before you enter your default state in ui-router.

Reference: https://github.com/angular-ui/ui-router/wiki#resolve

  • But using ui-router, I'll need resolve all controllers that use my factory of parameters? And I'll need refactor my $routeProvider native of Angular for $stateProvider of ui-router? – Railson Lima Apr 04 '17 at 11:34