Skip to content

Teach directives with a lesson nav - #41

Merged
gpbsumido merged 4 commits into
ps/ng-09-change-detectionfrom
ps/ng-10-directives
Nov 21, 2026
Merged

gpbsumido merged 4 commits into
ps/ng-09-change-detectionfrom
ps/ng-10-directives

Conversation

@gpbsumido

@gpbsumido gpbsumido commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Tenth lesson in my Angular guide: directives. The feature slice is an "On this page" nav on every lesson. It jumps to a section and highlights the one in view.

What shipped

A nav on every lesson.

  • It sticks to the top of the reader and lists the lesson's sections.
  • Pressing an entry scrolls to that heading and moves focus to it, since headings get tabindex="-1".
  • The section in view gets aria-current="location".
  • Smooth scrolling switches off under prefers-reduced-motion; browsers don't do that on their own.
  • The entries are buttons, not links. With <base href="/">, href="#simple" resolves to /#simple, and routerLink would need a router in every lesson spec.

ScrollSpy (lessons/scroll-spy.ts). An attribute directive that owns an IntersectionObserver.

  • It's created in afterNextRender, so prerender never sees it, and disconnected through DestroyRef.
  • The section in view is exposed as a signal.
  • Between headings it keeps the last section rather than clearing.
  • Lesson takes it as a host directive, so no lesson writes an attribute for it.

LessonSection (lessons/lesson-section.ts). A directive with selector h2, the same trick NgForm uses on <form>.

  • It gives each heading an id from its text and registers it with the ScrollSpy it injects. Element injectors follow the template, so a heading projected into <app-lesson> finds it.
  • Lesson lists the headings with contentChildren, so the nav and the ids are in the prerendered HTML.
  • The label and id are getters: a directive is created before its element's text node exists.

Lesson 10: directives (/thoughts/angular-10-directives).

  • Covers the material at three levels (simple, intermediate, expert), then pitfalls and interview Q&A:
    • attribute vs structural directives
    • * desugaring
    • element selectors
    • host directives with aliased inputs
    • directives finding each other through DI
    • DOM work and SSR
  • The live demos are a structural directive on TemplateRef/ViewContainerRef and a hostDirectives chip with a renamed input.

Version and changelog. 1.4.9, with a matching changelog entry.

Commits

  1. Add failing specs for a lesson scroll-spy nav and lesson 10
  2. Add an On this page nav to every lesson
  3. Write lesson 10 with structural and host directive demos
  4. Bump to 1.4.9 with the lesson 10 changelog entry

Verification

  • RED first. The every-lesson spec failed for all nine existing lessons, with no nav entries. The ScrollSpy, Lesson and lesson 10 specs didn't compile.
  • Spec changes since the RED commit.
    • The scrollIntoView mock in lesson.spec.ts needed its real type.
    • app.routes.spec.ts assumed every guide title starts "Angular 0", which stopped being true at lesson 10. It now matches two digits.
  • Tests. ng test: 64 files, 451 tests, exit 0. That includes the ScrollSpy unit specs against a fake IntersectionObserver, the nav on all ten lessons, and axe on the nav and the lesson 10 demos.
  • Static checks. Lint and both tsc --noEmit configs are clean.
  • Production build. Exit 0, 79 routes prerendered.
    • Lessons 01 and 10 both prerender with the nav and the ids simple, intermediate, expert, pitfalls and interview-questions.
    • Lesson 10 shows "Part 10 of 10".
  • Bundle. The initial bundle went from 392.64 to 394.56 kB, still under the 400 kB budget.
    • All the new code is in lazy chunks. The growth is the shared @angular/core chunk: this is the first use of contentChildren, hostDirectives and afterNextRender, and core ships as one flat module.
    • Another 0.35 kB is the lesson's metadata entry.

TO-DOs for this PR

  • Before/after screenshots of a lesson with the nav, scrolled to the middle, light and dark. Chromium couldn't launch in the environment I built this in.
  • Check the highlight in a real browser. The spec drives a fake observer, so the 30% trigger band hasn't been tuned against real scrolling.

Every lesson should get an "On this page" nav over its sections that
marks the one in view. ScrollSpy, LessonSection and lesson 10 don't
exist yet; the every-lesson spec fails nine times with no nav at all.
LessonSection matches each h2 in a lesson, gives it an id from its text
and registers it with ScrollSpy, which the lesson component takes as a
host directive. ScrollSpy creates its IntersectionObserver in
afterNextRender, so prerender never touches it, and highlights the
section in view. The lesson spec's scrollIntoView mock needed a type.
The tag page spec assumed every guide title starts "Angular 0", which
stopped being true at lesson 10; it now matches two digits.
@gpbsumido
gpbsumido marked this pull request as ready for review November 21, 2026 16:20
@gpbsumido
gpbsumido added this pull request to stack #46 November 21, 2026 17:00
@gpbsumido
gpbsumido merged commit 530c68f into develop Nov 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant