3
votes

This is very confusing in the documentation because one part suggests that loading a service in a module providers array will make it available to the whole application and if we need to isolate this service the only way is including that in the providers of a top parent component not the feature module providers.

When a module is loaded at application launch, its @NgModule.providers have application-wide scope. They are available for injection throughout the application.

Load the module lazily if you can. Angular gives a lazy-loaded module its own child injector. The module's providers are visible only within the component tree created with this injector.

If you must load the module eagerly, when the application starts, provide the service in a component instead.

https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-component-scoped-providers

On the other hand there is another part in the docs that says otherwise and that for isolating a service we can just add it to the module's providers array and that will isolate it to this specific feature module.

The CrisisService is neither needed nor wanted outside the Crisis Center domain. Instead of registering it with the AppModule's providers — which makes it visible everywhere — we register the CrisisService in the CrisisCenterModule providers array.

This limits the scope of the CrisisService to the Crisis Center routes. No module outside of the Crisis Center can access it.

https://angular.io/docs/ts/latest/guide/router.html#!#child-routing-component

Which concept is the right one ??

1
I ran across that second quote a few times and wondered to myself the same thing. Personally, I just chalked it being either a typo, a brain fart on part of the author, or an early draft that was never updated. More clearly, I think the second one is wrong. If the CrisisCenterModule was lazy loaded loaded, then it would be correct, but 1) it isn't, and 2) They make no mention of it being lazy loaded. The whole wording of it makes it seem like all modules get their own scope, which is not correct - Paul Samsotha
I haven't seen it mentioned but when the module is lazy loaded, the providers don't become globally available, only within the scope of the lazy loaded module. - Günter Zöchbauer
peeskillet I think you are right the router documentation was there before the NgModule and the NgModule FAQ section so I thought too it was a mistake just wanted to make sure I am not losing my brain. - Joseph Girgis
Günter Zöchbauer ya I agree it would be correct if the module was lazy loaded but that was not the case in this example. - Joseph Girgis
What's the point with having a forRoot() method that returns a moduleWithProviders? Is that if the module is lazyloaded and you want the providers to be available in a global state? - bjorkblom

1 Answers

2
votes

Both concepts are right. Defining a service in your NgModule causes that you can use that service as singleton.

But if you define the service into an independent component, angular will create a new instance of that service.