Service is a broad category encompassing any value, function, or feature that an application needs. A service is typically a class with a narrow, well-defined purpose. A component is one type of class that can use DI.
Angular distinguishes components from services to increase modularity and reusability. By separating a component's view-related features from other kinds of processing, you can make your component classes lean and efficient.
Ideally, a component's job is to enable the user experience and nothing more. A component should present properties and methods for data binding, to mediate between the view (rendered by the template) and the application logic (which often includes some notion of a model).
A component can delegate certain tasks to services, such as fetching data from the server, validating user input, or logging directly to the console. By defining such processing tasks in an injectable service class, you make those tasks available to any component. You can also make your application more adaptable by configuring different providers of the same kind of service, as appropriate in different circumstances.
Angular does not enforce these principles. Angular helps you follow these principles by making it easy to factor your application logic into services and make those services available to components through DI.
Service examples
Here's an example of a service class that logs to the browser console:
src/app/logger.service.ts (class)
export class Logger { log(msg: unknown) { console.log(msg); } error(msg: unknown) { console.error(msg); } warn(msg: unknown) { console.warn(msg); }}
Services can depend on other services.
For example, here's a HeroService
that depends on the Logger
service, and also uses BackendService
to get heroes.
That service in turn might depend on the HttpClient
service to fetch heroes asynchronously from a server:
src/app/hero.service.ts (class)
export class HeroService { private heroes: Hero[] = []; constructor( private backend: BackendService, private logger: Logger) {} async getHeroes() { // Fetch this.heroes = await this.backend.getAll(Hero); // Log this.logger.log(`Fetched ${this.heroes.length} heroes.`); return this.heroes; }}
Creating an injectable service
The Angular CLI provides a command to create a new service. In the following example, you add a new service to an existing application.
To generate a new HeroService
class in the src/app/heroes
folder, follow these steps:
- Run this Angular CLI command:
ng generate service heroes/hero
This command creates the following default HeroService
:
src/app/heroes/hero.service.ts (CLI-generated)
import { Injectable } from '@angular/core';@Injectable({ providedIn: 'root',})export class HeroService {}
The @Injectable()
decorator specifies that Angular can use this class in the DI system.
The metadata, providedIn: 'root'
, means that the HeroService
is provided throughout the application.
Add a getHeroes()
method that returns the heroes from mock.heroes.ts
to get the hero mock data:
src/app/heroes/hero.service.ts
import { Injectable } from '@angular/core';import { HEROES } from './mock-heroes';@Injectable({ // declares that this service should be created // by the root application injector. providedIn: 'root',})export class HeroService { getHeroes() { return HEROES; }}
For clarity and maintainability, it is recommended that you define components and services in separate files.
Injecting services
To inject a service as a dependency into a component, you can use the component's constructor()
and supply a constructor argument with the dependency type.
The following example specifies the HeroService
in the HeroListComponent
constructor.
The type of heroService
is HeroService
.
Angular recognizes the HeroService
type as a dependency, since that class was previously annotated with the @Injectable
decorator:
src/app/heroes/hero-list.component (constructor signature)
constructor(heroService: HeroService)
Injecting services in other services
When a service depends on another service, follow the same pattern as injecting into a component.
In the following example, HeroService
depends on a Logger
service to report its activities:
src/app/heroes/hero.service.ts
import { Injectable } from '@angular/core';import { HEROES } from './mock-heroes';import { Logger } from '../logger.service';@Injectable({ providedIn: 'root',})export class HeroService { constructor(private logger: Logger) {} getHeroes() { this.logger.log('Getting heroes.'); return HEROES; }}
In this example, the getHeroes()
method uses the Logger
service by logging a message when fetching heroes.