Repository navigation
docs: add guidance against calling methods in templates - #71941
Open
JeevanMahesha wants to merge 1 commit into
Open
JeevanMahesha wants to merge 1 commit into
JeevanMahesha wants to merge 1 commit into
Conversation
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 fuller section to components.md that recommends computed(), a pure pipe or a child component instead, with an example for the nested @for case. Signal reads and trivial lookups stay allowed. Related to angular#70132
JeevanMahesha
force-pushed
the
docs/skill-avoid-function-calls-in-templates
branch
from
November 11, 2026 05:37
fb3b201 to
6d7b81f
Compare
JeanMeche
reviewed
Nov 11, 2026
Comment on lines
+123
to
+142
| ```ts | ||
| // Avoid: items() runs again for every group on every check. | ||
| groups = computed(() => [this.count(), 1, 1]); | ||
| items(count: number) { | ||
| return Array(count); | ||
| } | ||
| ``` | ||
|
|
||
| ```html | ||
| @for (count of groups(); track $index) { @for (_ of items(count); track $index) { ... } } | ||
| ``` | ||
|
|
||
| ```ts | ||
| // Prefer: derive the nested data once. | ||
| groups = computed(() => [this.count(), 1, 1].map((n) => Array.from({length: n}, (_, i) => i))); | ||
| ``` | ||
|
|
||
| ```html | ||
| @for (group of groups(); track $index) { @for (item of group; track item) { ... } } | ||
| ``` |
Member
There was a problem hiding this comment.
I don't think there is much value in those examples
JeanMeche
reviewed
Nov 11, 2026
| @@ -108,6 +108,39 @@ The `@for` block iterates over collections. The `track` expression is **required | |||
|
|
|||
| **Implicit Variables**: `$index`, `$count`, `$first`, `$last`, `$even`, `$odd`. | |||
|
|
|||
| ### Avoiding function calls in templates | |||
|
|
|||
| Do not call component methods from template expressions, especially methods that take arguments or build a new array or object (e.g. `@for (x of getItems(count))`, `{{ formatDate(item.date) }}`). A method is re-run every time the component's template is checked, which happens on every change when the component is dirty (OnPush and zoneless reduce how often, but do not make the call cached). Methods that return a new array or object also give `@for` a new collection on every run. | |||
Member
There was a problem hiding this comment.
I think there is little value here too in giving examples.
And as mentionned in the issue, calling an method is not an issue per se.
What agent need to be careful is invoking expensive functions on every change detection cycle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Several reports on #70132 show AI coding agents using the
angular-developerskill writing method calls inside template expressions, for example@for (_ of items(count); track $index), whereitems()builds a new array on every check. The skill had no guidance on this, so agents had nothing telling them to avoid it.There is no maintainer consensus that every function call in a template is wrong. Signal reads are function calls, and with OnPush and zoneless a cheap call can be fine. So the guidance targets methods that take arguments or return new arrays/objects, and explicitly allows signal reads and trivial lookups.
What
SKILL.md: a short rule in the Components section, which agents read whenever they use the skill, linking to the details.references/components.md: a new "Avoiding function calls in templates" section that explains why it matters, lists alternatives (computed(), a pure pipe, a child component), and shows a before/after for the nested@forcase from the issue. Acomputed()cannot take arguments, so the example derives the whole nested structure once.Markdown-only change, so no automated tests apply. This is guidance for agents and has not been evaluated against agent output, so it is linked to the issue rather than closing it.
Related to #70132. Supersedes #70168, which was closed because its author had not signed the CLA.