Angular10 min read

Angular Services and Dependency Injection

Master Angular services and dependency injection. Learn how to create services, inject them, and share data across components. Essential for building maintainable Angular applications.

Lisa Anderson
December 18, 2025
0.0k0

Services are where you put business logic, data access, and shared functionality. Angular's dependency injection makes using services easy and testable. Understanding this is crucial for Angular development.

What are Services?

Services are TypeScript classes that provide functionality across your application. They're perfect for API calls, data sharing, logging, and any logic that multiple components need.

Dependency Injection

Angular's DI system automatically provides services to components that need them. You don't create services manually - Angular does it for you. This makes code testable and maintainable.

Creating Services

Use @Injectable decorator to mark a class as a service. Register it in providers array or use providedIn: 'root' for app-wide singleton. I'll show you both approaches.

Sharing Data

Services are perfect for sharing data between components that aren't parent-child. Store data in services and inject them where needed. This is cleaner than prop drilling.

Best Practices

I'll show you how to structure services, when to use them, and how to make them testable. These patterns will make your code professional and maintainable.

#Angular#Services#Dependency Injection#DI

Common Questions & Answers

Q1

What are Angular services and how do I create them?

A

Services are TypeScript classes decorated with @Injectable that provide functionality across the app. Use providedIn: 'root' for app-wide singleton, or add to providers array for specific scope. Inject services via constructor. Used for API calls, data sharing, and business logic.

typescript
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'  // App-wide singleton
})
export class UserService {
  private apiUrl = 'https://api.example.com/users';
  
  constructor(private http: HttpClient) {}
  
  getUsers() {
    return this.http.get(this.apiUrl);
  }
  
  getUser(id: number) {
    return this.http.get(`${this.apiUrl}/${id}`);
  }
}

// Use in component
@Component({
  selector: 'app-users'
})
export class UsersComponent {
  users: any[];
  
  constructor(private userService: UserService) {}
  
  ngOnInit() {
    this.userService.getUsers().subscribe(users => {
      this.users = users;
    });
  }
}
Q2

How does Angular dependency injection work?

A

Angular DI system automatically resolves and provides dependencies. Declare dependencies in constructor, Angular creates instances and injects them. Services are singletons by default when providedIn: 'root'. Use providers array to customize scope. Makes code testable by allowing easy mocking.

typescript
// Service
@Injectable({
  providedIn: 'root'
})
export class DataService {
  getData() {
    return 'Data from service';
  }
}

// Component - DI via constructor
@Component({
  selector: 'app-example'
})
export class ExampleComponent {
  // Angular automatically injects DataService
  constructor(private dataService: DataService) {}
  
  ngOnInit() {
    const data = this.dataService.getData();
    console.log(data);
  }
}

// Custom provider (component level)
@Component({
  selector: 'app-custom',
  providers: [DataService]  // New instance for this component
})
export class CustomComponent {}