2

I have the following plunker

This is the part to pay special attention:

export function startupServiceFactory(startupService: StartupService): Promise<any> {
  console.log('on startupService', startupService);  // (1)
  return () => {
    console.log("on return service load()"); // (2)
    return startupService.load()
  };
}

You will be able to see in the console that even when the first console.log (1) call comes prior to the one from the module's constructor, the next one right below the return () => doesn't (2).

I need to get a certain configuration from the server so then I can initialize a service from the module's constructor with that same data. Am I missing something or is this just not possible? How can I make a call to the server before anything else in an Angular app?

EDIT: By comment request, I'll try to explain a bit better: I'd need to postpone the execution of the module constructor so I can get certain configuration from the server prior to continue the execution of the application.

Please, don't mistake this question with this or this, as these don't deal with how to make the load occur before the module's constructor execution, they just explain the basic use of APP_INITIALIZER token.

CesarD
  • 573
  • 14
  • 30
  • Module construction isn't and can't be postponed. That's why modules classes are there - to carry information you're providing in NgModule meta to the app. These are app bootstrapping and module ngDoBootstrap that are postponed. If you have some problem with module construction not being postponed, please, make the question reflect that. – Estus Flask Aug 22 '17 at 17:37
  • Yes, correct, I would need to postpone the execution of the module constructor so I can load certain configuration from the server prior to continue with the rest of the application execution. I'll modify the answer accordingly so it's more clear to understand. – CesarD Aug 22 '17 at 17:41
  • You have XY problem. Module construction doesn't prevent you from doing that in any way. If you have problems with config loading in some particular implementation, please provide it. – Estus Flask Aug 22 '17 at 17:46
  • why don't you perform everything you need and only then call `boostrapPlatform`? – Max Koretskyi Aug 22 '17 at 19:11
  • @estus: I have modified the plunker to show more explicitly what I'm trying to do. You'll see the `StartupService` trying to get data from a file `config.json`. In the console you'll notice that when I try to log its value from the AppModule's constructor, that the User value is *undefined* at that point. That's pretty much what I'm trying to do in my real app. @MaximKoretskyi: how would you do that? Could you show me an example in a plunkr? Thanks both. – CesarD Aug 22 '17 at 19:40
  • The solution is obvious: don't use StartupService in module constructor. Module constructors don't serve this purpose. Why did you inject it there in the first place? – Estus Flask Aug 22 '17 at 19:47
  • I need the service to bring me that and other configuration data in several places of the application. So first, I need it to load it, and then I can use it to retrieve it anywhere in the application. There is a specific service initialization that I need to do in the module constructor of my app, so I need to inject the service to retrieve that data. What other way do you suggest to do it? Could you please elaborate an answer so I can mark it as answered if it solves the problem? Thanks. – CesarD Aug 22 '17 at 20:40
  • As your correctly deduced, module constructor it runs before anything else - that's its purpose and that's why you will probably never need a one. *specific service initialization* - which one? `startupService.load()` should be done **in** APP_INITIALIZER. What were the problems you encountered? Considering that you have a specific problem, the question is too vague about it. Here https://plnkr.co/edit/wZCHvV9Ij6YbvkKBjtUx?p=preview `startSvc.getLoggedInUser` is available in App component. That's how APP_INITIALIZER works. – Estus Flask Aug 22 '17 at 22:10
  • I'm using this library: https://github.com/damienbod/angular-auth-oidc-client . As you can see, to set it up it requires to do some initialization of its service in the AppModule's constructor. Given that the authority server (STS server) will change from one environment to the other during the release cycle, I need to take it from a configuration file, and this is where the app_initializer was expected to kick in to help me solve this case. – CesarD Aug 23 '17 at 04:33
  • @CesarD Did you get the solution? If yes, could you please share? I am having the same issue. – shobhit vaish Dec 05 '18 at 04:01

0 Answers0