Hone logo
Hone
Problems

Custom Angular Structural Directive: Conditional Rendering

This challenge focuses on creating a custom structural directive in Angular that conditionally renders a DOM element. Structural directives are powerful tools for manipulating the DOM's structure, and building one will deepen your understanding of Angular's template syntax and directive lifecycle.

Problem Description

You are tasked with creating a structural directive named appIf that behaves similarly to Angular's built-in ngIf. This directive should allow you to conditionally add or remove an element from the DOM based on a boolean expression.

Key Requirements:

  1. Directive Creation: Create a new Angular structural directive.
  2. Conditional Rendering: The directive should take a boolean expression as input. If the expression evaluates to true, the host element should be rendered. If it evaluates to false, the host element should be removed from the DOM.
  3. Template Handling: The directive must correctly handle the template it is applied to, potentially including other directives or child components.
  4. No Else Block: For this challenge, you do not need to implement an "else" block. The directive should simply not render anything if the condition is false.

Expected Behavior:

When an element has the appIf directive applied with a truthy expression, the element and its contents should be present in the DOM. When the expression is falsy, the element and its contents should be completely removed from the DOM, not just hidden.

Edge Cases:

  • What happens if the input expression changes dynamically? The directive should re-evaluate the expression and update the DOM accordingly.
  • Consider the initial rendering when the component loads.

Examples

Example 1: Basic Usage

Component Template:

<p *appIf="showGreeting">Hello, World!</p>
<button (click)="showGreeting = !showGreeting">Toggle Greeting</button>

Component Class:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  showGreeting: boolean = true;
}

Output:

  • Initially: The p element "Hello, World!" is displayed.
  • After clicking the button: The p element is removed from the DOM.
  • After clicking again: The p element reappears.

Explanation: The showGreeting property controls the rendering of the p element. When showGreeting is true, the appIf directive allows the p element to be added to the DOM. When it's false, the directive removes it.

Example 2: Using a more complex condition

Component Template:

<div *appIf="user && user.isAdmin">
  Welcome, Administrator!
</div>

Component Class:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  user = { name: 'Alice', isAdmin: true };
}

Output:

  • The div element "Welcome, Administrator!" is displayed.

Explanation: The appIf directive evaluates the user && user.isAdmin expression. Since both parts are true, the div is rendered. If user was null or user.isAdmin was false, the div would not be rendered.

Constraints

  • The directive must be implemented in TypeScript.
  • Use Angular's ViewContainerRef and TemplateRef to manipulate the DOM.
  • The directive should be a standalone directive or declared within an Angular module.
  • Avoid using ngIf in your directive's implementation.

Notes

  • Think about how structural directives are applied to elements using the asterisk (*) syntax. This is syntactic sugar for a common pattern.
  • You will need to inject ViewContainerRef and TemplateRef into your directive's constructor.
  • Consider using the OnChanges lifecycle hook if you need to react to changes in the input property.
  • The core functionality will involve inserting or removing the TemplateRef into/from the ViewContainerRef.
Loading editor...
typescript