Course lesson

Create an Angular Service to Retrieve Data from an API

In this lesson we will create the ProductsService that is responsible for the interaction with our API.

Duration
2 min
Access
Pro
Transcript
Available

In this lesson we will create the ProductsService that is responsible for the interaction with our API.

We will inject the HttpClient that we get from @angular/common/http. To make this working we need to import HttpClientModule in our AppModule.

We define a getProducts and getProduct method so we can get a list of products and an single product which we will use in respectively the ProductList and ProductDetail components.

In both components we will use the ngOnInit lifecycle hook to invoke the methods on the service, and retrieve the data by subscribing to it.

Link to product api used in lessons