Skip to main content

Creating a Service

Angular services are fundamental to any Angular app. Services are a great way to share information among classes that don't know each other. In this tutorial, we will guide you through the steps of creating an Angular service from scratch. By the end of this guide, you should have a solid understanding of how services work in Angular.

What is a Service?

In Angular, a service is a class with a specific purpose. It's a broad category encompassing any value, function, or feature that your application needs. A service can be anything ranging from a value, function, or feature that your application needs.

The main objective of a service is to organize and share code across your application. Angular services are substitutable objects that are wired together using dependency injection (DI). You can use a service to share data or to implement any custom functionality that you need for your project.

Steps to Create a Service in Angular

Let's dive into the steps you need to follow to create a service in Angular.

Step 1: Creating a Service

To create a service in Angular, you can use the Angular CLI command ng generate service or ng g s for short. Let's create a service named my-service.

ng generate service my-service

When you run the above command, Angular will generate two files:

  • my-service.service.ts - This is the actual service file where you'll write your logic.
  • my-service.service.spec.ts - This is the testing file associated with the service.

Step 2: Writing a Service

Open the my-service.service.ts file. You'll see a class MyServiceService with an @Injectable decorator. The @Injectable decorator is what makes it a service.

import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class MyServiceService {

constructor() { }

}

Now, let's add a function getData() that just returns a simple message.

import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class MyServiceService {

constructor() { }

getData() {
return "Hello from MyService!";
}

}

Step 3: Using a Service

To use this service, you need to import it into the component where you want to use it. Let's say we want to use it in app.component.ts.

First, import the service.

import { MyServiceService } from './my-service.service';

Next, inject the service into the constructor of AppComponent.

constructor(private myService: MyServiceService) { }

Now, you can use the getData() function of myService anywhere in the AppComponent.

ngOnInit() {
console.log(this.myService.getData());
}

When you run this code, "Hello from MyService!" will be logged to the console.

Conclusion

Congratulations! You've created your first Angular service. Remember, services are a crucial part of Angular apps to share code across different components. They help in maintaining the code DRY (Don't Repeat Yourself) and organized.

Keep practicing creating and using services, as they are a fundamental part of Angular and a powerful tool in your Angular toolbox. Happy coding!