Hone logo
Hone
Problems

Dynamic Content Display with Angular ng-template

Angular's ng-template allows you to define reusable chunks of HTML that are not rendered initially but can be dynamically inserted into the DOM based on conditions. This challenge focuses on creating a component that utilizes ng-template to display different content based on a user-selected option, demonstrating a fundamental technique for building dynamic and flexible Angular applications.

Problem Description

You are tasked with creating an Angular component called DynamicContentComponent that displays different content based on a user's selection. The component should have a dropdown (select element) allowing the user to choose between three options: "Option A", "Option B", and "Option C". Each option should be associated with a distinct piece of content defined within ng-template directives. When the user selects an option, the corresponding ng-template should be rendered, and the others should remain hidden.

Key Requirements:

  • Dropdown: A <select> element with three <option> elements: "Option A", "Option B", and "Option C".
  • ng-templates: Three distinct ng-template directives, each containing different HTML content representing "Option A", "Option B", and "Option C".
  • Conditional Rendering: The selected option from the dropdown should control which ng-template is displayed. Use *ngIf or similar conditional rendering directives to show only the template corresponding to the selected option.
  • Data Binding: The selected option should be bound to a component property.
  • Component Structure: The solution should be a well-structured Angular component with appropriate TypeScript code.

Expected Behavior:

  1. Initially, no content should be displayed (all templates hidden).
  2. When the user selects "Option A" from the dropdown, the content associated with the "Option A" template should be displayed.
  3. When the user selects "Option B" from the dropdown, the content associated with the "Option B" template should be displayed.
  4. When the user selects "Option C" from the dropdown, the content associated with the "Option C" template should be displayed.
  5. Changing the selection should dynamically update the displayed content.

Edge Cases to Consider:

  • Initial value of the dropdown: Consider setting a default value for the dropdown and ensuring the corresponding template is initially hidden.
  • Empty selection: Handle the case where the dropdown might be empty (though not explicitly required, it's good practice).

Examples

Example 1:

Input: Dropdown selection: "Option A"
Output: Displays content associated with "Option A" template. Other templates are hidden.
Explanation: The component correctly renders the content defined within the "Option A" ng-template.

Example 2:

Input: Dropdown selection: "Option B"
Output: Displays content associated with "Option B" template. Other templates are hidden.
Explanation: The component correctly renders the content defined within the "Option B" ng-template.

Example 3:

Input: Dropdown selection: "Option C"
Output: Displays content associated with "Option C" template. Other templates are hidden.
Explanation: The component correctly renders the content defined within the "Option C" ng-template.

Constraints

  • Angular Version: Assume Angular version 14 or higher.
  • TypeScript: The solution must be written in TypeScript.
  • HTML Structure: The HTML structure should be clean and well-formatted.
  • Performance: The solution should be performant; avoid unnecessary DOM manipulations. The conditional rendering should be efficient.
  • Component Size: Keep the component relatively small and focused on the core task.

Notes

  • Consider using *ngIf for conditional rendering within the HTML template.
  • The component should be self-contained and reusable.
  • Think about how to handle the initial state of the dropdown and the corresponding template.
  • Focus on demonstrating the use of ng-template for dynamic content display. Styling is not a primary concern for this challenge.
  • You can use any Angular best practices you are familiar with.
Loading editor...
typescript