I am trying to build a simple shopping cart application. I have two components and a cart service like following.
<
In angular the components will share an instance of a service if it is from the same ancestor. For example you have your app-header
and app-cart
wich are part of the AppModule
. So if you add SharedCartService
to the providers
array of your module, the two components will get the same instance of that service.
Dependency injection and hierarchical dependency injection