Implementing Constructor Injection in Angular Components
Constructor injection is a fundamental design pattern in Angular that promotes testability, reusability, and maintainability by providing dependencies to a component through its constructor. This challenge will guide you through creating an Angular component that utilizes constructor injection to receive a service, demonstrating a best practice for dependency management.
Problem Description
You are tasked with creating a simple Angular component called DataDisplayComponent that displays data fetched from a service called DataService. The DataDisplayComponent should receive an instance of DataService via constructor injection. The component should then call a method on the DataService to retrieve data and display it in the component's template. The goal is to demonstrate how to properly inject a service into a component's constructor, allowing for easy testing and decoupling.
Key Requirements:
- Create a
DataServicethat has a method calledgetData()which returns a Promise resolving to an array of strings. For simplicity, the data can be a hardcoded array of strings. - Create a
DataDisplayComponentthat injects theDataServiceinto its constructor. - In the component's
ngOnInitlifecycle hook, callgetData()on the injectedDataService. - Store the retrieved data in a component property.
- Display the data in the component's template using
*ngFor.
Expected Behavior:
When the component loads, it should fetch data from the DataService and display it in a list. If the DataService encounters an error, the component should display an error message.
Edge Cases to Consider:
- What happens if the
DataServicethrows an error? The component should handle this gracefully and display an appropriate error message. - Consider the asynchronous nature of the
getData()method. Ensure the component waits for the data to be fetched before attempting to display it.
Examples
Example 1:
Input: DataService.getData() returns ["Item 1", "Item 2", "Item 3"]
Output: The DataDisplayComponent template displays a list:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
Explanation: The component successfully fetched the data and rendered it in the template.
Example 2:
Input: DataService.getData() throws an error.
Output: The DataDisplayComponent template displays an error message:
<p>Error fetching data.</p>
Explanation: The component correctly handled the error and displayed an error message to the user.
Constraints
- The solution must be written in TypeScript.
- The
DataServiceshould use a Promise to simulate asynchronous data fetching. - The component should use Angular's built-in
*ngFordirective to display the data. - Error handling should be implemented to gracefully handle potential errors from the
DataService. - The solution should be modular and follow Angular best practices.
Notes
- Remember that constructor injection provides a clean and testable way to manage dependencies.
- Consider using Angular's
asyncpipe in the template to handle the asynchronous data fetching. - Think about how to handle potential errors from the
DataServiceand provide a user-friendly error message. - Focus on demonstrating the core concept of constructor injection and its benefits. No complex styling or additional features are required.