NG.IV · Angular 22 practice question

A developer has a component, `ParentComponent`, which provides a specific instance of `LoggerService`. This component renders a child component, `ChildComponent`. If `ChildComponent` requests `LoggerService`, what is the guaranteed behavior regarding the instance it receives?

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

@Component({ selector: 'app-parent', standalone: true, template: '<div><app-child></app-child></div>' })
export class ParentComponent { provide: { LoggerService: LoggerService } };

@Component({ selector: 'app-child', standalone: true, template: '<div>...</div>' })
export class ChildComponent { constructor(public logger: LoggerService) {} };
Choose one answer
Check answer and explanation

Correct answer: The child component will receive the specific instance provided by the parent component, demonstrating override capability.

Explanation

The child component will receive the specific instance provided by the parent component, demonstrating override capability. This illustrates that child components can override providers from parent components.

Common trap

The assertion that the child component will always receive a newly created instance, regardless of the parent's provider, is incorrect because the hierarchy ensures the nearest provider is checked first. Furthermore, the claim that the child component will search up the entire application tree until it finds the first available instance, ignoring the parent's local provider, is wrong because the hierarchy dictates that the local injector is checked before moving up.

Source: Angular docs: Defining dependency providers — Injector hierarchy in Angular
Checked against the cited text on 2026-11-25.

Report an error in this question