Hone logo
Hone
Problems

Angular Hierarchical Injection Challenge: Building a Nested Service

This challenge focuses on implementing hierarchical injection in Angular, a powerful feature allowing services to be injected into child components based on the injector of their parent. Understanding hierarchical injection is crucial for creating modular, reusable, and testable Angular applications, particularly when dealing with complex component trees and shared state. You'll build a scenario where a parent component provides a service, and child components can access and utilize that service without explicitly declaring it in their own modules.

Problem Description

You need to create a parent component (ParentComponent) and a child component (ChildComponent) that demonstrates hierarchical injection. The ParentComponent will provide a custom service (SharedService) using its injector. The ChildComponent, which is a child of ParentComponent, should be able to inject SharedService without being declared in any module or having its own provider. The SharedService should have a method getData() that returns a string indicating its origin (either "Parent" or "Child"). The ChildComponent should call getData() and display the result.

Key Requirements:

  • SharedService: A simple service with a getData() method.
  • ParentComponent: Provides SharedService in its injector.
  • ChildComponent: Injects SharedService and calls getData(). The ChildComponent should not declare SharedService as a provider.
  • Display: The ChildComponent must display the result of getData() in its template.
  • Correct Origin: The getData() method in SharedService must return "Parent" when called from ParentComponent and "Child" when called from ChildComponent.

Expected Behavior:

When the application runs, the ChildComponent should display "Child". This confirms that the ChildComponent successfully received the SharedService from its parent's injector.

Edge Cases to Consider:

  • What happens if the ParentComponent doesn't provide the service? (The ChildComponent should throw an error during compilation).
  • Ensure the getData() method correctly identifies the origin of the call.

Examples

Example 1:

Input: ParentComponent provides SharedService, ChildComponent injects SharedService.
Output: ChildComponent displays "Child".
Explanation: The ChildComponent inherits the injector from the ParentComponent, allowing it to access the SharedService provided by the ParentComponent. The SharedService's getData() method correctly identifies the origin as "Child".

Example 2:

Input: ParentComponent does *not* provide SharedService.
Output: Angular throws a compilation error in ChildComponent indicating that SharedService could not be found.
Explanation:  Hierarchical injection relies on the parent providing the service.  Without it, the child cannot resolve the dependency.

Constraints

  • The solution must be written in TypeScript.
  • The solution must use Angular version 14 or higher.
  • The solution should be modular and well-structured.
  • The getData() method in SharedService should use a mechanism to determine the origin of the call (e.g., checking the component's constructor).
  • No external libraries beyond Angular are allowed.

Notes

  • Consider using Angular's dependency injection mechanism effectively.
  • Think about how the SharedService can differentiate between calls from the parent and child components. A simple way is to check if the component constructor has a reference to the parent component.
  • Focus on demonstrating the core concept of hierarchical injection – the child component receiving a service from its parent without explicit declaration.
  • The goal is to create a minimal, working example that clearly illustrates the principle.
Loading editor...
typescript