Injecting the service
The last step for our service is to inject it into the component that needs it. When a component needs a service instance, it defines the dependency for the said service, and Angular is responsible for resolving the dependency. This process is called dependency injection, wherein the class just defines the dependency it needs and it receives the dependencies from the external source rather than creating it itself.
As Angular creates a single instance of the service and is responsible for injecting it into components, we can use the service to share data between the components. Angular injects the dependencies into the component at the time of object creation of the component. Hence, it makes sense to have dependency ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access