1

I want to create NewService. And I want to have possibility to call it from all application?

Where I should inject it in SharedModule (module which exports all reusable modules) or in AppModule ( main module of my application)

@NgModule({

  providers: [
     // Here?
  ]
})
John Doe
  • 3,794
  • 9
  • 40
  • 72

1 Answers1

3

It doesn't matter if you add it to the AppModule directly or to a feature module as long as the module is not lazy loaded.

Lazy loaded modules have their own root scope and providers added there are only visible to the lazy loaded module.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Sure. Pack directives, pipes, and services together if they build a reusable feature together and then add the module to `imports: [...]` where you want to use this feature. This is the intended use of `NgModule` (in addition to lazy loading). – Günter Zöchbauer Nov 23 '16 at 14:59
  • So, I should add NewService to exports: [...] in SharedModule and import SharedModule to another modules. But I can not use NewService in another module? What else have I to do? – John Doe Nov 23 '16 at 16:39
  • Services should be enough to add to `providers: []` only directives, components, and pipes and re-exported modules need to be added to `exports: []` and then add this module to `imports: []` of the AppModule or some other module where you want to use it. – Günter Zöchbauer Nov 23 '16 at 16:41