Hone logo
Hone
Problems

Implementing ngOnChanges in Angular for Dynamic Updates

Angular's ngOnChanges lifecycle hook is crucial for responding to changes in input properties of a component. This challenge will guide you in implementing ngOnChanges to dynamically update a component's state based on changes to its input properties, ensuring your component reacts appropriately to external data modifications. Understanding and utilizing ngOnChanges is fundamental for building reactive and data-driven Angular applications.

Problem Description

You are tasked with creating an Angular component called DataDisplayComponent that displays data received through an input property called data. The component should implement the ngOnChanges lifecycle hook to react to changes in the data input. When the data input changes, the component should log a message to the console indicating that the data has been updated, along with the new data value. Furthermore, the component should display the received data in a designated <div> element.

Key Requirements:

  • Implement the ngOnChanges lifecycle hook.
  • Access the changed input property (data) within ngOnChanges.
  • Log a message to the console whenever data changes, including the new value.
  • Display the data value within a <div> element in the component's template.
  • Handle the case where data is initially undefined or null.

Expected Behavior:

  1. When the component is initialized, it should not log anything to the console.
  2. When the data input is first set, the component should log a message to the console like: "Data updated: [new data value]".
  3. When the data input changes subsequently, the component should log a similar message each time.
  4. The <div> element in the template should display the current value of the data input.
  5. If the initial value of data is undefined or null, the <div> should display an appropriate message (e.g., "No data available").

Edge Cases to Consider:

  • Initial value of data being undefined or null.
  • data being an object or array. The component should handle these data types gracefully.
  • Multiple input properties – focus solely on the data property for this challenge.

Examples

Example 1:

Input: Parent component sets data = "Hello, World!";
Output: Console log: "Data updated: Hello, World!"
Explanation: The component receives the string "Hello, World!" as input and logs the update to the console. The div displays "Hello, World!".

Example 2:

Input: Parent component sets data = { name: "John", age: 30 };
Output: Console log: "Data updated: { name: 'John', age: 30 }"
Explanation: The component receives an object as input and logs the update to the console. The div displays "[object Object]".

Example 3:

Input: Parent component sets data = null;
Output: Console log: "Data updated: null"
Explanation: The component receives null as input and logs the update to the console. The div displays "No data available".

Constraints

  • The component must be a standard Angular component.
  • The data input property must be of type any.
  • The component should be performant; avoid unnecessary computations within ngOnChanges.
  • The component should be easily testable.

Notes

  • The SimpleChanges object passed to ngOnChanges provides information about the changed properties. You can access the previous and current values of the input property using simpleChanges['data'].previous and simpleChanges['data'].current.
  • Consider how to handle the initial state of the component when data is undefined or null.
  • Focus on the core functionality of reacting to changes in the data input and displaying it. Styling and complex data transformations are outside the scope of this challenge.
Loading editor...
typescript