Hone logo
Hone
Problems

Managing Shared Services and Dependencies in Angular

Angular applications often require services to be accessible across multiple components. This challenge focuses on creating and injecting a shared service that manages a simple counter, demonstrating best practices for dependency injection and ensuring the service's lifecycle is properly managed within an Angular application. This is crucial for maintaining code organization, reusability, and testability.

Problem Description

You need to create an Angular application with a shared service called CounterService. This service should have a single property, count, initialized to 0, and a method increment() that increases the count by 1. Two components, ComponentA and ComponentB, should both inject and utilize the CounterService. ComponentA should have a button that, when clicked, calls increment() on the service. ComponentB should display the current value of count from the service. The application should demonstrate that changes made to count in ComponentA are reflected in ComponentB.

Key Requirements:

  • Create an Angular project (you can assume this is already done for the challenge).
  • Implement the CounterService with the count property and increment() method.
  • Create ComponentA with a button that calls CounterService.increment() on click.
  • Create ComponentB that displays the current value of CounterService.count.
  • Ensure both components successfully inject the CounterService.
  • Verify that clicking the button in ComponentA updates the displayed count in ComponentB.

Expected Behavior:

  1. The application starts with ComponentB displaying "0".
  2. Clicking the button in ComponentA increments the counter.
  3. ComponentB immediately updates to display the new count value.
  4. Repeated clicks on the button in ComponentA continue to increment the counter and update the display in ComponentB.

Edge Cases to Consider:

  • Ensure the service is properly injected and available in both components.
  • Consider the lifecycle of the service and how it interacts with the components. The service should persist across component interactions.

Examples

Example 1:

Input: ComponentA button click (initial state: count = 0)
Output: ComponentB displays "1"
Explanation: ComponentA calls CounterService.increment(), increasing count to 1. ComponentB reads and displays the updated count.

Example 2:

Input: ComponentA button click (state: count = 5)
Output: ComponentB displays "6"
Explanation: ComponentA calls CounterService.increment(), increasing count to 6. ComponentB reads and displays the updated count.

Constraints

  • The solution must be written in TypeScript.
  • The Angular version should be 14 or higher.
  • The solution should be modular and well-structured, following Angular best practices.
  • The solution should not use any external libraries beyond those typically included in an Angular project.
  • The solution should be testable (although writing tests is not required for this challenge).

Notes

  • Focus on demonstrating proper dependency injection and service lifecycle management.
  • Consider using the @Component and @Injectable decorators appropriately.
  • Think about how Angular's change detection mechanism will affect the display in ComponentB.
  • You can use Angular's template syntax (e.g., {{ count }}) to display the counter value.
  • The challenge assumes a basic Angular project setup is already in place. You don't need to create the entire project from scratch.
Loading editor...
typescript