typescript
71 lines · 9 steps
Building a content-projected wizard in Angular
A wizard component discovers its steps through content projection and renders one step's template at a time.
Explained by
highlit
1import {
2 Component,
3 ContentChildren,
4 QueryList,
5 AfterContentInit,
6 Input,
7 TemplateRef,
8 Directive,
9} from '@angular/core';
10
11@Directive({ selector: '[wizardStepContent]', standalone: true })
12export class WizardStepContentDirective {
13 constructor(public readonly template: TemplateRef<unknown>) {}
14}
15
16@Component({
17 selector: 'wizard-step',
18 standalone: true,
19 template: '',
20})
21export class WizardStepComponent {
22 @Input({ required: true }) label!: string;
23 @Input() optional = false;
24 @ContentChildren(WizardStepContentDirective) content!: QueryList<WizardStepContentDirective>;
25}
26
27@Component({
28 selector: 'wizard',
29 standalone: true,
30 imports: [WizardStepContentDirective],
31 template: `
32 <nav class="wizard-nav">
33 @for (step of steps; track step.label; let i = $index) {
34 <button
35 type="button"
36 class="wizard-tab"
37 [class.active]="i === activeIndex"
38 [disabled]="i > activeIndex"
39 (click)="activeIndex = i"
40 >
41 {{ i + 1 }}. {{ step.label }}
42 @if (step.optional) { <em>(optional)</em> }
43 </button>
44 }
45 </nav>
46
47 <section class="wizard-body">
48 @if (active(); as current) {
49 <ng-container *ngTemplateOutlet="current.content.first!.template"></ng-container>
50 }
51 </section>
52 `,
53})
54export class WizardComponent implements AfterContentInit {
55 @ContentChildren(WizardStepComponent) private stepList!: QueryList<WizardStepComponent>;
56
57 steps: WizardStepComponent[] = [];
58 activeIndex = 0;
59
60 ngAfterContentInit(): void {
61 this.steps = this.stepList.toArray();
62 this.stepList.changes.subscribe(() => {
63 this.steps = this.stepList.toArray();
64 this.activeIndex = Math.min(this.activeIndex, this.steps.length - 1);
65 });
66 }
67
68 active(): WizardStepComponent | undefined {
69 return this.steps[this.activeIndex];
70 }
71}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1ContentChildren lets a parent component discover projected children declaratively without the caller wiring them up.
- 2Wrapping content in a structural directive captures a TemplateRef you can render lazily with ngTemplateOutlet.
- 3Subscribing to QueryList.changes keeps derived state correct when projected children are added or removed at runtime.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express'; import { DataSource } from 'typeorm';
Per-tenant database connections in NestJS
multi-tenancy
connection-pooling
dependency-injection
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-content-projected-wizard-in-angular-explained-typescript-578a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.