Hone logo
Hone
Problems

Angular Dependency Injection Context: A Practical Challenge

Dependency Injection (DI) is a cornerstone of Angular development, enabling modularity, testability, and maintainability. This challenge focuses on understanding and utilizing Angular's injection contexts – Hierarchical, Component, and Module – to correctly provide dependencies to components at different levels of the application tree. Successfully completing this challenge demonstrates a solid grasp of Angular's DI system.

Problem Description

You are tasked with building a simple Angular application that demonstrates the different injection contexts. The application should consist of a root module, a component, and a child component. The root module will provide a service, and the challenge is to correctly inject this service into both the root component and the child component, showcasing how the injection context affects the instance of the service received.

Specifically, you need to:

  1. Create a Root Module (AppModule): This module will provide a service called RootService. RootService should have a method getRootMessage() that returns a string like "Message from RootService".
  2. Create a Root Component (AppComponent): This component should inject RootService and display the message returned by getRootMessage() in its template.
  3. Create a Child Component (ChildComponent): This component should also inject RootService. It should display a different message, indicating that it has received the service. The message should be "Message from ChildComponent using RootService".
  4. Inject RootService into AppComponent: Ensure that AppComponent receives an instance of RootService provided by AppModule.
  5. Inject RootService into ChildComponent: Ensure that ChildComponent receives an instance of RootService provided by AppModule. This demonstrates the Hierarchical injection context.
  6. Verify Instance: Both components should use the same instance of RootService. You can verify this by adding a counter property to RootService that increments each time a method is called. Both components calling their respective methods should increment the same counter.

Expected Behavior:

  • The AppComponent template should display "Message from RootService".
  • The ChildComponent template should display "Message from ChildComponent using RootService".
  • The counter in RootService should increment by 2 (1 from AppComponent and 1 from ChildComponent).

Examples

Example 1:

Input: AppModule provides RootService; AppComponent and ChildComponent inject RootService.
Output: AppComponent displays "Message from RootService"; ChildComponent displays "Message from ChildComponent using RootService"; RootService.callCount is 2.
Explanation: Both components receive the same instance of RootService provided by AppModule, demonstrating hierarchical injection.

Example 2:

Input: AppModule provides RootService; AppComponent injects RootService; ChildComponent attempts to provide its own RootService.
Output: AppComponent displays "Message from RootService"; ChildComponent displays "Message from ChildComponent using RootService"; RootService.callCount is 2.
Explanation: ChildComponent's attempt to provide its own RootService is ignored because AppModule already provides it in the hierarchy.

Constraints

  • The application must be a functional Angular application.
  • Use TypeScript for all code.
  • The solution should be concise and well-structured, adhering to Angular best practices.
  • The counter in RootService should be a private property.
  • The application should not use any external libraries beyond Angular itself.
  • The solution should be testable (although writing tests is not explicitly required for this challenge).

Notes

  • Consider the different injection contexts (Hierarchical, Component, and Module) and how they affect the instance of the service received.
  • Pay close attention to where you are providing the service and where you are injecting it.
  • The key to this challenge is understanding that components within the same hierarchy inherit the injection context of their parent modules.
  • Think about how Angular resolves dependencies when multiple providers exist. In this case, the provider higher up in the hierarchy takes precedence.
  • Use the providedIn: 'root' option in your service to make it available throughout the application.
Loading editor...
typescript