3

I have two modules - let's call them module-E and module-P. I have one service in module-E say service-E which has multiple methods. I want to use all the functionality of service-E in module-P except for a few functions so I have extended service-P from service-E and have overridden those functions in service-P.

In module-E the provider declaration is

providers: [service-E]

In module-P the provider declaration is

providers: [{provide: service-E, useClass: servie-P}]

The problem is if I load module-P first, I get service-P even in module-E and if I load module-E first then I get service-E in module-P.

I want to restrict service-P to module-P and service-E to module-E no matter what.

Siddhant Swami
  • 323
  • 1
  • 3
  • 14

2 Answers2

0

You can't restrict them, because all services share the same injector, so whenever you load a service (for example from lazy loaded module) the service becomes globally available.

Source: https://medium.com/@michelestieven/organizing-angular-applications-f0510761d65a

0

Services provided in the modules are registered with the root injector. So, the first instance that you get will be shared in the whole application.

But, if the modules are lazy loaded, they'll both have a separate instance for each module.

Refer : https://angular.io/guide/dependency-injection#injectable-ngmodule-or-component

Akshay Rana
  • 1,455
  • 11
  • 20