Hone logo
Hone
Problems

Building Reusable UI Components with Angular Elements

Angular Elements allow you to create Angular components that can be used outside of Angular applications, such as in web components, React, or vanilla JavaScript environments. This challenge will guide you through creating a simple Angular Element that displays a greeting message, demonstrating the core concepts of element creation and usage within an Angular application. This is a valuable skill for building truly reusable UI components and integrating with diverse frameworks.

Problem Description

You are tasked with creating an Angular Element called greeting-element. This element should accept an @Input() property called name and display a greeting message incorporating the provided name. The element should be a simple, self-contained component that can be easily integrated into any web page.

What needs to be achieved:

  1. Create a new Angular component named GreetingComponent.
  2. Define an @Input() property named name in GreetingComponent.
  3. Display a greeting message in the component's template, using the value of the name input. The message should be "Hello, [name]!".
  4. Create an Angular Element from the GreetingComponent.
  5. Demonstrate the usage of the created element within an Angular application. Specifically, create a simple Angular app component that includes the greeting-element and passes a name to it.

Key Requirements:

  • The name input must be properly defined and used in the template.
  • The Angular Element must be created correctly and registered with the browser.
  • The Angular application must successfully render the greeting-element with the provided name.
  • The code should be clean, well-commented, and follow Angular best practices.

Expected Behavior:

When the Angular application loads, the greeting-element should be rendered. The element should display the greeting message "Hello, [name]!", where [name] is the value passed to the name input property. If no name is provided, the element should display "Hello, World!".

Edge Cases to Consider:

  • What happens if the name input is null or undefined? The element should gracefully handle this and display a default greeting.
  • Ensure the element is properly registered and available in the browser's custom element registry.

Examples

Example 1:

Input:  Angular application with greeting-element and name="Alice"
Output:  The greeting-element displays "Hello, Alice!"
Explanation: The component receives "Alice" as the value for the 'name' input and renders the corresponding greeting.

Example 2:

Input: Angular application with greeting-element and no name input provided.
Output: The greeting-element displays "Hello, World!"
Explanation: Since no name is provided, the component defaults to displaying "Hello, World!".

Example 3:

Input: Angular application with greeting-element and name=null
Output: The greeting-element displays "Hello, World!"
Explanation: The component handles the null input and defaults to displaying "Hello, World!".

Constraints

  • The Angular version should be 14 or higher.
  • The solution must be written in TypeScript.
  • The element should be self-contained and not rely on external dependencies beyond Angular itself.
  • The Angular application should be a simple, functional example demonstrating the element's usage.
  • The solution should be easily understandable and maintainable.

Notes

  • You'll need to use the @Component and @Input decorators.
  • Remember to register the element using createCustomElement.
  • Consider using defineCustomElement to register the element globally.
  • Think about how to handle the case where the name input is not provided. A default value is a good approach.
  • Start by creating the component and then focus on creating the element. Test each step before moving on.
Loading editor...
typescript