Services in Angular

What is a Service?

A service is typically a class with a narrow, well-defined purpose. Angular provides flexibility to factor your application logic into services and make those services available to components through dependency injection. Service classes are decorated with @Injectable() decorator to show they can be injected to some other services or components.

What is dependency injection?

Dependency Injection (DI) is a way to create objects that depend upon other objects. A Dependency Injection system supplies the dependent objects (called the dependencies) when it creates an instance of an object.

What is @Injectable?

The @Injectable decorator identifies services and other classes that are intended to be injected. It can also be used to configure a provider for those services. It is basically a marker metadata that marks a class as available to Injector for creation.

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

@Injectable()
export class AngularService {
  constructor() { }
}