Hone logo
Hone
Problems

Implementing a ViewChild Signal in Angular

Angular's Signals provide a reactive way to manage component state. This challenge focuses on leveraging Signals with ViewChild to reactively track and access a DOM element or component instance within a template. This is particularly useful for scenarios where you need to dynamically interact with a child element based on its availability or state changes.

Problem Description

You need to create an Angular component that utilizes a ViewChild and a Signal to reactively track the availability of a child element. The component should:

  1. Have a child component named ChildComponent that exposes a property isReady which is initially false and becomes true after a short delay (simulating initialization).
  2. Use ViewChild to access an instance of ChildComponent.
  3. Create a Signal named childReadySignal that emits true when the ChildComponent's isReady property is true.
  4. Display a message in the parent component's template based on the value of childReadySignal. The message should be "Child is not ready" until the signal emits true, at which point it should display "Child is ready!".

Key Requirements:

  • The childReadySignal must be reactive to the ChildComponent's isReady property.
  • The parent component's template must dynamically update based on the childReadySignal.
  • The solution should be idiomatic Angular and utilize Signals effectively.

Expected Behavior:

When the parent component is initialized, the template should display "Child is not ready". After a short delay (simulated by the ChildComponent), the ChildComponent's isReady property will become true. This change should trigger the childReadySignal to emit true, and the parent component's template should update to display "Child is ready!".

Edge Cases to Consider:

  • What happens if the ChildComponent is not present in the template? (Handle this gracefully, perhaps by initializing the signal to false.)
  • How to ensure the ViewChild is properly initialized before accessing its properties? (Use afterViewInit lifecycle hook.)

Examples

Example 1:

Input: Parent component with ChildComponent present in the template. ChildComponent's isReady becomes true after 1 second.
Output: Initially displays "Child is not ready". After 1 second, displays "Child is ready!".
Explanation: The childReadySignal reacts to the ChildComponent's isReady property and updates the template accordingly.

Example 2:

Input: Parent component with ChildComponent absent from the template.
Output: Displays "Child is not ready".
Explanation: The childReadySignal is initialized to false and remains false, as the ChildComponent is not available.

Constraints

  • The solution must be written in TypeScript.
  • The delay in ChildComponent should be between 500ms and 1500ms.
  • The solution should be a complete, runnable Angular component.
  • Avoid using async/await directly within the childReadySignal computation for simplicity. Use zoneChange or similar techniques if needed to trigger reactivity.

Notes

  • Consider using takeUntilDestroyed() on the signal to prevent memory leaks.
  • The ViewChild query should be configured to target the ChildComponent specifically.
  • Think about how to handle the case where the ChildComponent might be conditionally rendered.
  • Focus on creating a clean and reactive solution that demonstrates the power of Signals in Angular.
Loading editor...
typescript