Angular incident
Cart badge and cart page drift out of sync
Failure signals
The clues you start with
Overview
What failed in production?
Start by making the bug legible: what the user saw, why it mattered, and what the product was doing underneath.
Symptom
User impact
Environment
CartService uses providedIn: 'root', but the cart feature shell also lists providers: [CartService]. The header badge and the cart page both inject the service.
Evidence pack
What you can inspect before answering
Visible mismatch
Runtime clue
CartService instance ids: one in the app shell and one inside the cart feature.
Cart feature shell
@Injectable({ providedIn: 'root' })
export class CartService {
items: CartItem[] = [];
}
@Component({
selector: 'app-cart-shell',
templateUrl: './cart-shell.component.html',
providers: [CartService]
})
export class CartShellComponent {
constructor(public cart: CartService) {}
}Stage 1: likely root cause
Why are the badge and cart page out of sync?
detectChanges(), so the right cart state never reaches the template.providedIn: 'root' always guarantees one instance everywhere, so DI cannot be the issue.Stage 2: debug priority order
What would you check first? Put the best three at the top.
- #1
Log the CartService instance identity in the header and in the cart feature. This proves whether the two screens share the same service. - #2
Inspect route, feature, and component providers to see where CartService is re-provided. You need to know which injector branch owns the duplicate instance. - #3
Reproduce the mismatch by adding an item in one screen and reading the count in the other without a full reload. Confirm it is really a shared-state split, not a one-off timing issue. - #4
Replace the whole cart flow with NgRx before confirming the service scope problem. Too large and too early. - #5
Scatter detectChanges()calls until both screens appear to match.Masks the symptom without proving the root cause.
Only the top 3 positions are scored.
Expected:
Expected:
Expected:
Stage 3: best fix set
Which changes belong in the actual fix?
providers: [CartService] so the cart feature uses the root instance.Stage 4: regression guard
What is the best guardrail for this bug?
You noticed the mismatch, but not the injector boundary
You were close, but one fix still treated the symptom
You diagnosed the duplicate service instance clearly
Ideal runbook
How a strong answer should flow
- 1
Prove whether both screens use the same CartService instance. - 2
Inspect where CartService is provided and which injector branch shadows the root service. - 3
Remove the accidental duplicate provider or scope it intentionally with a different contract. - 4
Protect the fix with a cross-screen shared-state test.
Reflection
Reflection note
Angular shortcut
Strong answer signals
You mention hierarchical DI, not just generic state bugs. You prove the duplicate instance before changing architecture. You restore one clear source of truth instead of syncing two copies.