From 09264663affa30134efe13014734574e2ca89629 Mon Sep 17 00:00:00 2001 From: Jeevan Mahesha <34814862+JeevanMahesha@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:00:57 +0530 Subject: [PATCH] docs: add guidance against expensive work in templates AI coding agents using the angular-developer skill were observed calling plain methods inside @for/@if template expressions, for example items(count) returning a new array on every check. The skill had no guidance on this. Add a short rule to SKILL.md and a section to components.md that recommends computed(), a pure pipe or a child component for expensive work that would otherwise re-run on every change detection cycle. Signal reads and cheap calls stay allowed. Related to #70132 --- skills/dev-skills/angular-developer/SKILL.md | 2 ++ .../angular-developer/references/components.md | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/skills/dev-skills/angular-developer/SKILL.md b/skills/dev-skills/angular-developer/SKILL.md index ca401f2c21d..3457b4680a7 100644 --- a/skills/dev-skills/angular-developer/SKILL.md +++ b/skills/dev-skills/angular-developer/SKILL.md @@ -54,6 +54,8 @@ When working with Angular components, consult the following references based on - **Deferrable Views**: Lazy loading, code splitting, triggers, prefetching, loading states, SSR, and layout stability with `@defer`. Read [deferrable-views.md](references/deferrable-views.md) - **Error Boundaries**: Catching render and change detection errors in a template subtree with `@boundary`/`@error`, retrying with `$reset`. Read [error-boundary.md](references/error-boundary.md) +**Template expressions**: Avoid expensive work in templates (e.g. building arrays or objects, filtering, sorting), since it re-runs on every change detection cycle. Reading signals (`count()`) and cheap calls are fine. Derive other values with `computed()`, a pure pipe, or a child component. See [Avoiding expensive work in templates](references/components.md#avoiding-expensive-work-in-templates). + If you require deeper documentation not found in the references above, read the documentation at `https://angular.dev/guide/components`. ## Reactivity and Data Management diff --git a/skills/dev-skills/angular-developer/references/components.md b/skills/dev-skills/angular-developer/references/components.md index fad773adc29..d73c52901be 100644 --- a/skills/dev-skills/angular-developer/references/components.md +++ b/skills/dev-skills/angular-developer/references/components.md @@ -108,6 +108,16 @@ The `@for` block iterates over collections. The `track` expression is **required **Implicit Variables**: `$index`, `$count`, `$first`, `$last`, `$even`, `$odd`. +### Avoiding expensive work in templates + +Template expressions are re-evaluated every time the component's template is checked, so avoid doing expensive work in them (e.g. building or filtering arrays, sorting, heavy formatting). Calling a cheap method is fine; the concern is expensive work repeated on every change detection cycle. A method that returns a new array or object also hands `@for` a new collection on every run. + +Reading a signal (`count()`, `items()`) is fine, and so is a trivial lookup. For anything expensive, derive the value once: + +- **`computed()`** for values derived from signals. +- **A pure pipe** for formatting a value (see [pipes.md](pipes.md)). +- **A child component** when each item needs its own derived state. + ### Switching Content (`@switch`) The `@switch` block renders content based on a value. It uses strict equality (`===`) and has **no fallthrough**.