Introduction to Angular Services

In this Angular Services tutorial, we will show you how to build a simple component that fetches a list of products from an Angular Service and displays it in our template

Our components need to access to data. You can write data access code in each component, but that is very inefficient and breaks the rule of single responsibility. The Component must focus on presenting data to the user. The task of getting data from the back end server must be delegated to some other class. Such a class is called service class. Because it provides the service of providing data to every component that needs it.

Applies to: Angular 2 to the latest edition of i.e. Angular 8. Angular 9, Angular 10, Angular 11

What is a Service

Service is a piece of reusable code with a focused purpose. A code that you will use it in many components across your application

What services are used for

  1. Features that are independent of components such a logging services
  2. Share logic or data across components
  3. Encapsulate external interactions like data access

Advantageous of Service

  1. Services are easier to test.
  2. Services are easier to Debug.
  3. You can reuse the service.

How to create a Service in Angular

An Angular service is simply a Javascript function. All we need to do is to create a class and add methods & properties. We can then create an instance of this class in our component and call its methods.

One of the best uses of services is to get the data from the data source. Let us create a simple service, which gets the product data and passes it to our component.

You can download the source code from gitHub.

Best Angular Books
The Top 8 Best Angular Books, which helps you to get started with Angular  


Create a new file under the folder src/app and call it product.ts

The Product class above is our domain model.

Product Service

Next, let us build an Angular Service, which returns the list of products.

Create a new file under the folder src/app and call it product.service.ts

First, we have imported the Product

We have created the ProductService class and exported it.

We have created a getProducts method, which returns the collection of the products. In this example, we have hard coded the products. In real life, you would send an HTTP request to your back end API to get the data

Now our service is ready.

Note that the above class is a simple JavaScript function. There is nothing Angular about it.

Invoking the ProductService

The Next step is to invoke the ProductService from the component. Open the app.componet.ts and add the following code.

First, we have imported both Product & ProductService

In the constructor of the AppComponet, we have created the instance of the ProductSerivce

The getProducts method calls the getProducts method of the ProductService. The returned list of Products are stored in the local variable products


The next step is to display the Products to user

Open the app.component.html file and add the following code

We are using the bootstrap 3 to style our template here

We have added a button “Get Products”, which is hooked to getProducts method of the component class via event binding

We are displaying the returned products via ngFor directive.

Finally, run the code and click on the Get Product button and see the Products are displayed.

Injecting Services into Component

In the example, we instantiated the productService in the Component directly as shown below

Directly instantiating the service, as shown above, has many disadvantageous

  1. The ProductService is tightly coupled to the Component. If we change the ProductService class definition, then we need to update every code where service is used
  2. If we want to change ProductService with BetterProductService, then we need to search wherever the ProductService is used and manually change it
  3. Makes Testing difficult. We may need to provide mockProductService for testing and use the ProductService for Production.

This problem can be solved by Angular Dependency injection. You can learn it from our next tutorial on Dependency Injection in Angular 2.

Suggested Reading
Angular Dependency Injection
Angular Injector
Angular Providers
Angular Hierarchical Dependency Injection

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top