Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What are Angular Services?

 In Angular, a Service is a Class that is used to provide shared features across different components. Components are the View layer they deal only with UI specific features. Functional tasks like ajax calls, business logic, validations, logging, etc are delegated to the services.

Services are a great way to share information among Components that don't know each other. Based on how we register a service we can control the Scope / visibility of the service across the Application. Services can be registered with 3 levels of scope

1. Global Scope
2. Module Scope
3. Component Scope


To create a service we can use the following ng command.
                ng generate service hello

This will create a template class for the service, as follows.

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

@Injectable({
  providedIn: 'root',
})

export class HelloService {
  constructor() { }
}

Notice that the new service imports the Angular Injectable symbol and annotates the class with the @Injectable() decorator. This marks the class as one that participates in the dependency injection system and allows us to inject an instance of the Service to Components and other classes in the Application. 


This post first appeared on C# Guide, please read the originial post: here

Share the post

What are Angular Services?

×

Subscribe to C# Guide

Get updates delivered right to your inbox!

Thank you for your subscription

×