Sandboxing Components with Multiple Instances

Learn how to provide an Angular service dedicated to a component, resulting in a unique service instance for each component.

When we provide a service through the component injector and inject it into the component’s constructor, a new instance is created every time the component is rendered on the page. It can come in handy in cases such as when we want to have a local cache service for each component. We will explore this scenario by transforming our Angular application so that the product list displays a quick view of each product using an Angular service:

  1. Run the following command inside the src\app\products folder to create a new Angular component for the product view:

Note: The command below is for creating a component on the local machine using the Angular CLI.

Get hands-on with 1200+ tech skills courses.