typescript
40 lines · 7 steps
Multi-slot content projection in Angular
An Angular card component distributes projected markup into named slots using ng-content selectors.
Explained by
highlit
1import { Component, ChangeDetectionStrategy } from '@angular/core';
2
3@Component({
4 selector: 'app-card',
5 standalone: true,
6 changeDetection: ChangeDetectionStrategy.OnPush,
7 template: `
8 <article class="card">
9 <header class="card__header">
10 <ng-content select="[card-title]">
11 <span class="card__title--fallback">Untitled</span>
12 </ng-content>
13 <div class="card__actions">
14 <ng-content select="[card-actions]"></ng-content>
15 </div>
16 </header>
17
18 <div class="card__media">
19 <ng-content select="img, video, [card-media]"></ng-content>
20 </div>
21
22 <div class="card__body">
23 <ng-content></ng-content>
24 </div>
25
26 <footer class="card__footer">
27 <ng-content select="card-footer"></ng-content>
28 </footer>
29 </article>
30 `,
31 styles: [`
32 .card { display: flex; flex-direction: column; border: 1px solid #e2e2e2; border-radius: 12px; overflow: hidden; }
33 .card__header { display: flex; align-items: center; justify-content: space-between; padding: 1rem; }
34 .card__media img, .card__media video { width: 100%; display: block; }
35 .card__body { padding: 1rem; }
36 .card__footer:empty { display: none; }
37 .card__footer { padding: 0.75rem 1rem; border-top: 1px solid #f0f0f0; }
38 `],
39})
40export class CardComponent {}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Multiple ng-content elements with select attributes let one component route projected content into distinct regions.
- 2Fallback content inside ng-content renders only when nothing matches that slot, giving sensible defaults.
- 3OnPush change detection plus a purely presentational template makes a component that only re-renders when inputs or projection change.
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/multi-slot-content-projection-in-angular-explained-typescript-8a71/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.