Providing Services

26 views
Skip to first unread message

Sergio Costas

unread,
Jun 26, 2019, 9:08:22 AM6/26/19
to Angular and AngularJS discussion
I just finished my first module, now I am working in my second module and realize I am gonna need a service from the first module, I was wondering if I can Provide it in the @NgModule({providers}) list of the second module, I am afraid angular creates two instances of the service if I provide it in two modules.

Sander Elias

unread,
Jun 30, 2019, 3:58:41 AM6/30/19
to Angular and AngularJS discussion
Hi Sergio,

That is exactly what will happen. Use `{provideIn:root'}` on your services, and don't put them in modules. This will make them "globally" to your app, and the CLI is smart enough to bundle them in the most efficient way.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages