Custom Structural Directive
Structural directives are built using the <ng-template> concept. To create a custom structural directive, you inject TemplateRef and ViewContainerRef and apply logic to create or clear embedded views.
Example: a custom *appTimes directive
Section titled “Example: a custom *appTimes directive”This directive repeats the content a specified number of times.
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({selector: '[appTimes]',standalone: true})export class TimesDirective {constructor(private templateRef: TemplateRef<any>,private viewContainer: ViewContainerRef) {}
@Input() set appTimes(count: number) {this.viewContainer.clear();for (let i = 0; i < count; i++) {this.viewContainer.createEmbeddedView(this.templateRef, {$implicit: i,index: i});}}}**Usage in template:**
```jsimport { Component } from "@angular/core";import { TimesDirective } from "./times.directive";
@Component({ selector: "app-structural-demo", standalone: true, imports: [TimesDirective], template: ` <div *appTimes="5; let i; let idx = index">Item {{ i }} (index: {{ idx }})</div> `,})export class StructuralDemoComponent {}Explanation
Section titled “Explanation”TemplateRefholds the template content (what’s inside the element with the directive).ViewContainerRefis a container where we can attach the template multiple times.createEmbeddedViewcreates an embedded view from the template, optionally passing a context object.- The context object defines variables that can be used in the template:
$implicitis the default value forlet var, and additional variables likeindexcan be bound withlet idx = index.
The asterisk syntax
Section titled “The asterisk syntax”The *appTimes="5; let i; let idx = index" is desugared to:
<ng-template [appTimes]="5" let-i let-idx="index"> <div>Item {{ i }} (index: {{ idx }})</div></ng-template>Note: The context object’s $implicit property is used when the template uses let var without a right‑hand side.