Reputation: 5387
We are just on our first project with angular2. I've got a question about providing a service.
As I know, there are two ways to declare a provider with providers:[MyService]
in your app.
You can declare it globally in the @NgModule
tag or locally in the @Component
tag.
As far as I know, the only difference between the two ways is the providing scope. Once app wide, once only component wide. Out of this I would make the conclusion, that I should prefer to provide a service mostly (depending on the usage of the service) local in my specific component to keep the scope small.
Is that correct, or are there any other differences between the two declaration ways, which I'm not aware of?
Upvotes: 5
Views: 1129
Reputation: 16917
If you provide a service local to your components they will NOT have the SAME service. They get all A service of the same type, but they will be not the SAME one.
If you provide them to your AppModule, the will be created as a singleton for the whole app.
Upvotes: 4