Creating a Reusable CommonModule in Angular
Angular applications often contain components, directives, and pipes that are used across multiple modules. To avoid code duplication and promote maintainability, it's best practice to encapsulate these shared elements within a dedicated CommonModule. This challenge asks you to create a CommonModule that includes a simple, reusable component, directive, and pipe.
Problem Description
You need to create an Angular CommonModule that exports a reusable HighlightDirective, a ReversePipe, and a GreetingComponent. The HighlightDirective should highlight the text of an element with a yellow background. The ReversePipe should reverse a given string. The GreetingComponent should display a greeting message that can be configured via an @Input() property. The module should be designed to be imported into other feature modules without causing any errors or unexpected behavior.
Key Requirements:
- Create a new Angular module named
CommonModule. - Implement the
HighlightDirectiveto apply a yellow background to the element it's applied to. - Implement the
ReversePipeto reverse a string. - Implement the
GreetingComponentwith an@Input()property namednameto customize the greeting. - Export the
HighlightDirective,ReversePipe, andGreetingComponentfrom theCommonModule. - Ensure the module is standalone.
Expected Behavior:
- When imported into another module, the
HighlightDirective,ReversePipe, andGreetingComponentshould be available for use. - The
HighlightDirectiveshould correctly highlight the text of elements. - The
ReversePipeshould correctly reverse strings. - The
GreetingComponentshould display a greeting message with the provided name.
Edge Cases to Consider:
- The
ReversePipeshould handle empty strings gracefully (return an empty string). - The
GreetingComponentshould handle null or undefined input for thenameinput property gracefully (e.g., display a default greeting).
Examples
Example 1:
Input (in a component using HighlightDirective): <p appHighlight>This text should be highlighted.</p>
Output: <p style="background-color: yellow;">This text should be highlighted.</p>
Explanation: The HighlightDirective applies a yellow background to the paragraph element.
Example 2:
Input (in a component using ReversePipe): {{ "hello" | reverse }}
Output: olleh
Explanation: The ReversePipe reverses the string "hello".
Example 3:
Input (in a component using GreetingComponent): <app-greeting name="Alice"></app-greeting>
Output: <app-greeting name="Alice">Hello, Alice!</app-greeting>
Explanation: The GreetingComponent displays "Hello, Alice!".
Constraints
- The
CommonModulemust be standalone. - The
HighlightDirectiveshould use inline styles for simplicity. - The
ReversePipemust handle empty strings correctly. - The
GreetingComponentmust handle null/undefinednameinput gracefully. - All code must be written in TypeScript.
Notes
- Consider using Angular's
@Directive,@Pipe, and@Componentdecorators. - Think about how to structure your module to ensure it's reusable and doesn't introduce unnecessary dependencies.
- Focus on creating a clean and well-organized module that adheres to Angular best practices.
- You don't need to create a full application; just the
CommonModuleand its contents are sufficient. You can use a simple app module to import and test the CommonModule.