What is a Service in Angular?
A Service is a class used to share data, logic, or functionality across components.
👉 Common uses:
-
API calls (HttpClient)
-
Business logic
-
Data sharing between components
-
Utility functions
-
Authentication
Syntax to create services:
ng generate service services/user
it creates
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor() { }
getUsers() {
return ['Shiva', 'Ram', 'Sita'];
}
}
What is
providedIn: 'root'?It means:
-
Service is available application-wide
-
Only one instance is created (Singleton)
-
Tree-shakable (removed if not used)
Dependency Injection (DI) in Angular
Dependency Injection means: Angular automatically creates and provides required dependencies.
Instead of doing this:
const userService = new UserService(); ❌
Angular does this internally:

0 Comments
POST Answer of Questions and ASK to Doubt