Creating a Reusable Shared Module in Angular
Angular applications often contain components, pipes, and directives that are used across multiple modules. Creating a shared module allows you to encapsulate these reusable elements, promoting code organization, maintainability, and reducing redundancy. This challenge will guide you through building a shared module that contains a custom pipe and a simple component.
Problem Description
You are tasked with creating a shared module named SharedModule in an Angular application. This module should contain:
- A Custom Pipe: A pipe named
ReversePipethat takes a string as input and returns the reversed string. - A Simple Component: A component named
DisplayMessageComponentthat accepts an@Input()property calledmessageand displays it within a<p>tag.
The SharedModule should be designed to be easily imported into other modules within the application without causing conflicts or unnecessary dependencies. The module should be declared as standalone to align with modern Angular practices.
Key Requirements:
- The
ReversePipemust correctly reverse any given string. - The
DisplayMessageComponentmust display the value of itsmessageinput property. - The
SharedModulemust be declared asstandalone. - The module should export both the
ReversePipeand theDisplayMessageComponentso they can be used in other modules.
Expected Behavior:
When another module imports SharedModule, it should be able to use both the ReversePipe and the DisplayMessageComponent without needing to declare them individually.
Edge Cases to Consider:
- Empty strings passed to
ReversePipe. - Null or undefined values passed to the
messageinput ofDisplayMessageComponent. (Handle gracefully, perhaps displaying a default message). - Non-string input to
ReversePipe. (Consider how to handle this - either throw an error or attempt to convert to a string).
Examples
Example 1:
Input (to ReversePipe): "hello"
Output: "olleh"
Explanation: The pipe reverses the input string "hello" to "olleh".
Example 2:
Input (to DisplayMessageComponent): message = "Welcome!"
Output: <p>Welcome!</p>
Explanation: The component displays the string "Welcome!" within a paragraph tag.
Example 3: (Edge Case)
Input (to ReversePipe): ""
Output: ""
Explanation: An empty string remains an empty string after reversal.
Constraints
- The solution must be written in TypeScript.
- The
SharedModulemust be declared asstandalone. - The code should be well-formatted and easy to understand.
- The
ReversePipeshould handle non-string inputs gracefully (either by converting to a string or throwing an error - document your choice). - The
DisplayMessageComponentshould handle null/undefined input gracefully.
Notes
- Consider using Angular's dependency injection system appropriately.
- Think about how to structure your code for maximum reusability and maintainability.
- Focus on creating a clean and well-defined shared module that can be easily integrated into other parts of your Angular application.
- You don't need to create a full Angular application; just the
SharedModule,ReversePipe, andDisplayMessageComponentare required. You can assume the existence of a basic Angular project structure.