Wednesday, August 12, 2020

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. 

Search Flipkart Products:
Flipkart.com

No comments: