Services and dependency injection
Move shared logic into injectable services and inject them where needed.
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.
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
EasyInject HttpClient
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 inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.