Angular·Angular from components to production

Services and dependency injection

Move shared logic into injectable services and inject them where needed.

Intermediate 13 min read Lesson 5 of 300 Challenge included
What you will be able to do
  • Create services with @Injectable.
  • Use providedIn: "root" for shared singletons.
  • Inject with the inject() function.
  • Keep HTTP and state logic out of components.

In plain English

A service is a class with a single responsibility. Angular’s injector creates it and hands it to consumers. Use providedIn: root for app-wide singletons and inject() for concise constructor-free injection.

courses.service.tsTYPESCRIPT
import { Injectable, inject } from '@angular/core'
import { HttpClient } from '@angular/common/http'

@Injectable({ providedIn: 'root' })
export class CoursesService {
  private http = inject(HttpClient)

  list() {
    return this.http.get<Course[]>('/api/courses')
  }
}
Your turn

Inject HttpClient

Easy

Add HttpClient to a service using inject().

TYPESCRIPT · starter
private http =
Check your understanding before moving on.
Knowledge checkQuestion 1

What does providedIn: "root" mean?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: RxJS and HTTP
Lesson 5 of 300 · 0% of course