Hone logo
Hone
Problems

Implementing ngAfterViewInit for DOM Manipulation

This challenge focuses on understanding and correctly implementing the ngAfterViewInit lifecycle hook in Angular. This hook is crucial for performing actions that require the component's view to be fully initialized and accessible, such as interacting with DOM elements or integrating with third-party libraries that need DOM access.

Problem Description

Your task is to create an Angular component that displays a list of items and then, after the component's view has been fully initialized, highlights the first item in the list using plain JavaScript.

Requirements:

  1. Create an Angular Component: Develop a new Angular component named HighlightListComponent.
  2. Display a List: The component should receive an array of strings as an input property (items) and display them as an unordered list (<ul>). Each item should be an <li> element.
  3. Implement ngAfterViewInit: Inside HighlightListComponent, implement the ngAfterViewInit lifecycle hook.
  4. DOM Manipulation: Within ngAfterViewInit, use native DOM manipulation to add a CSS class named highlight to the first <li> element of the displayed list.
  5. Provide CSS: Include a basic CSS rule for the highlight class (e.g., setting a background color).
  6. Component Usage: Demonstrate how to use HighlightListComponent in a parent component, passing an array of strings to its items input.

Expected Behavior:

When the HighlightListComponent is rendered in the browser, the first item in the displayed list should have a visual indication (e.g., different background color) due to the applied highlight class.

Edge Cases to Consider:

  • What happens if the items input is an empty array? The component should render without errors, and ngAfterViewInit should gracefully handle the absence of list items.
  • Ensure that the DOM manipulation targets the correct element and doesn't cause unintended side effects.

Examples

Example 1: Basic Usage

Parent Component Template (app.component.html):

<app-highlight-list [items]="myItems"></app-highlight-list>

Parent Component TypeScript (app.component.ts):

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  myItems = ['Apple', 'Banana', 'Cherry', 'Date'];
}

Expected Output (Visual):

An unordered list will be displayed. The first item, "Apple", will have a distinct styling (e.g., highlighted background).

Explanation: The AppComponent passes an array of strings to HighlightListComponent. HighlightListComponent renders these as list items, and its ngAfterViewInit hook applies the highlight class to the first <li> element.

Example 2: Empty List

Parent Component Template (app.component.html):

<app-highlight-list [items]="emptyItems"></app-highlight-list>

Parent Component TypeScript (app.component.ts):

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  emptyItems: string[] = [];
}

Expected Output (Visual):

An empty unordered list will be displayed. No errors should occur.

Explanation: When an empty array is provided, the HighlightListComponent's template will render an empty <ul>. The ngAfterViewInit hook will be called, but it should not find any <li> elements to manipulate, thus preventing errors.

Constraints

  • The solution must be implemented using Angular and TypeScript.
  • DOM manipulation should be done using native JavaScript APIs within the ngAfterViewInit hook. Avoid using Angular directives like ngClass for the highlighting itself (though you can use it for other purposes if needed).
  • The highlight CSS class should be defined in the component's stylesheet.
  • The solution should be efficient and not introduce significant performance overhead.

Notes

  • Remember that ngAfterViewInit is called only once after the component's view (and child views) has been fully initialized.
  • You will need to access the DOM elements rendered by your component's template. Consider using ViewChild or ElementRef if you need to reference specific elements, or query the DOM directly if you know the structure.
  • The order of lifecycle hooks is important. ngOnInit is too early for DOM manipulation that relies on rendered content.
  • Be mindful of rendering strategies. This challenge assumes server-side rendering is not a primary concern, focusing on client-side DOM interaction.
Loading editor...
typescript