typescript 48 lines · 7 steps

An infinite-scroll directive in Angular

An Angular directive fires loadMore when its host element scrolls into view, using IntersectionObserver kept off the change-detection loop.

Explained by highlit
1import {
2 Directive,
3 ElementRef,
4 EventEmitter,
5 Input,
6 NgZone,
7 OnDestroy,
8 OnInit,
9 Output,
10 inject,
11} from '@angular/core';
12 
13@Directive({
14 selector: '[appInfiniteScroll]',
15 standalone: true,
16})
17export class InfiniteScrollDirective implements OnInit, OnDestroy {
18 @Input() rootMargin = '200px';
19 @Input() threshold = 0;
20 @Input() disabled = false;
21 
22 @Output() readonly loadMore = new EventEmitter<void>();
23 
24 private readonly host = inject(ElementRef<HTMLElement>);
25 private readonly zone = inject(NgZone);
26 private observer?: IntersectionObserver;
27 
28 ngOnInit(): void {
29 this.zone.runOutsideAngular(() => {
30 this.observer = new IntersectionObserver(
31 (entries) => {
32 const [entry] = entries;
33 if (!entry?.isIntersecting || this.disabled) {
34 return;
35 }
36 this.zone.run(() => this.loadMore.emit());
37 },
38 { rootMargin: this.rootMargin, threshold: this.threshold },
39 );
40 
41 this.observer.observe(this.host.nativeElement);
42 });
43 }
44 
45 ngOnDestroy(): void {
46 this.observer?.disconnect();
47 }
48}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1IntersectionObserver detects visibility without scroll listeners or manual math.
  2. 2Running observers outside Angular's zone avoids change detection on every callback.
  3. 3Directives should disconnect observers in ngOnDestroy to prevent leaks.

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.

An infinite-scroll directive in Angular — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code