Services — Complete Guide
Services — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MEAN Stack Tutorial on Toolliyo Academy.
On this page
MEAN Stack Tutorial · Lesson 13 of 100
Services
Stack → Projects
Stack · 1 — Pieces · ~6 min · MEAN — Fundamentals
What is this?
Angular services are injectable classes holding shared logic — API calls, state, auth — used across MeanVerse components.
Why should you care?
Without services, each component duplicates HttpClient code and caches inconsistently.
See it live — copy this example
Paste into your MeanVerse project (Angular + Express + MongoDB), then run with ng serve / node / mongosh as noted.
@Injectable({ providedIn: 'root' })
export class AccountService {
private http = inject(HttpClient);
private base = '/api/accounts';
list(): Observable<Account[]> {
return this.http.get<Account[]>(this.base);
}
getById(id: string): Observable<Account> {
return this.http.get<Account>(`${this.base}/${id}`);
}
}
What happened?
- providedIn: 'root' registers a singleton.
- inject(HttpClient) avoids constructor boilerplate.
- Methods return Observables for async streams.
Practice next
- ng generate service core/account
- Inject AccountService in AccountListComponent
- Subscribe with async pipe in template
- Add shareReplay(1) for cached account list.
- Mock AccountService in component tests.
Remember
Services own API and shared business logic. providedIn root = one app-wide instance. Return Observables; let components use async pipe.
SaaS tenant context
MeanVerse TenantService holds current org id for all feature API calls.
Outcome: One service ensures every request sends X-Tenant-Id header.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!