1

If I want to isolate service in one module, I should create one root component for module, and use providers on this component (as said docs).

If I use providers on the module, service become application level. So, it is way to provide application-level services (as it made in example).

But, at the next, docs says:

Do not specify app-wide singleton providers in a shared module. A lazy loaded module that imports that shared module will make its own copy of the service.

So, we should not use providers of the modules to provide shared services. So, what should we use? AppModule providers? Should we provide all shared services in AppModule? When we should use providers of the feature modules?

Looks like, module providers are shared, but you should not use them as shared. WTF?

Pavel
  • 2,602
  • 1
  • 27
  • 34

2 Answers2

2

Do not specify app-wide singleton providers in a shared module. A lazy loaded module that imports that shared module will make its own copy of the service.

This only applies to lazy loaded modules. Providers in non-lazy loaded modules always are added to the application root scope.

Lazy loaded modules get their own root scope, because the root injector can't be modified later (after it was initialized).

To ensure that all services are provided at root level, implement the forRoot() method and provide the services there. Then import the service at the AppModule with imports: [MyModule.forRoot()]

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Hi Gunter, can you provide an example of how to implement `forRoot()`? I recently tried to play with this, but without success. – Stefan Svrkota Jan 29 '17 at 21:13
  • If you use lazy, then module providers should not be used, right? I would like to develop application architecture with lazy modules. I do not want to develop application, that works only without lazy modules. Should I always use forRoot for all shared services then? And, how to implement this also interesting. – Pavel Jan 29 '17 at 21:27
  • https://angular.io/docs/ts/latest/guide/ngmodule.html#!#core-for-root. shows how to do it. Yes, I think you should use `forRoot()` for all global providers. – Günter Zöchbauer Jan 30 '17 at 03:56
  • @GünterZöchbauer, there quote from provided link: >> "Call forRoot only in the root application module, AppModule. Calling it in any other module, particularly in a lazy loaded module, is contrary to the intent and is likely to produce a runtime error." Looks like, we should not use forRoot for global providers in feature modules. Therefore, if we provide service in AppModule, not in feature module, then problem is solved without forRoot (lazy-module will not import provider). So, should we just provide all shared services with AppModule? When to use feature modules providers then? – Pavel Jan 30 '17 at 05:55
  • yes,`forRoot()` should only be used in `AppModule`. Provide global servises either directly in `AppModule` or indirectly, using `forRoot()` – Günter Zöchbauer Jan 30 '17 at 06:36
  • No, indirectly is not a case: "Calling it in any other module, particularly in a lazy loaded module, is contrary to the intent and is likely to produce a runtime error.". I'm going to write issue to the Angular. Looks like feature module providers should not be used by design at all. – Pavel Jan 30 '17 at 06:42
  • That's not what I meant. See the first sentence in my last comment. – Günter Zöchbauer Jan 30 '17 at 06:46
1

It is Angular issue, that is still open.

Pavel
  • 2,602
  • 1
  • 27
  • 34