NG.IV · Angular 22 practice question

A developer is building a highly specialized checkout component that requires a unique caching mechanism for its current transaction state, ensuring that an instance on one checkout page does not affect another. To achieve this necessary isolation, where should the developer register the state management service?

import { Component } from '@angular/core';

@Component({
  selector: 'app-checkout',
  providers: [TransactionCacheService] // The service needing isolation
}) 
export class CheckoutComponent {}
Choose one answer
Check answer and explanation

Correct answer: Within the component's metadata providers array, to guarantee each instance of the component gets its own service copy.

Explanation

Registering the service in the component's metadata providers array ensures that 'Each component needs its own copy of the service,' fulfilling the requirement for isolated state.

Common trap

Registering it in the root NgModule providers array would cause all instances to share the same service instance, violating the isolation requirement. The root injector is similar to the root NgModule providers array in scope.

Source: Angular docs: Defining dependency providers — Component or directive providers
Checked against the cited text on 2026-11-25.

Report an error in this question