Sub topic 4
Dependencies are discovered at run time
This chapter assumes you know signal(), set() and update(). It also assumes you have used computed() to derive a value from other signals. You should be comfortable with basic TypeScript.
No dependency list to maintain
Some reactive systems make you list a computation's inputs, as with the dependency array in React's useEffect. Angular does not. When a computed() function runs, Angular records every signal it reads during that run. Those signals become the computed value's dependencies.
The dependency set belongs to the latest evaluation, not to the source code. If the function reads different signals on its next run, the set changes with it.
A computed with a branch
import { signal, computed } from '@angular/core';
const showDetails = signal(false);
const name = signal('Ada');
const details = signal('Mathematician');
const label = computed(() =>
showDetails() ? `${name()} - ${details()}` : name()
);
console.log(label()); // 'Ada'
details.set('Programmer'); // label is not affected while showDetails() is false
showDetails.set(true);
console.log(label()); // 'Ada - Programmer'Follow the first evaluation. showDetails() returns false, so the ternary takes the name() branch. The function never calls details(). The dependencies are showDetails and name.
When details.set('Programmer') runs, details is not a dependency of label. Nothing is marked stale, and the cached 'Ada' stays valid.
After showDetails.set(true), label is stale. The next read re-runs the function. This time it reads showDetails, name and details, so all three are now dependencies. The result is 'Ada - Programmer', and it already includes the earlier change to details.
Why this design is useful
Dynamic tracking has practical benefits:
- Less wasted work. A change to an unread signal does not invalidate the computed value.
- Safe guards. You can read an expensive or possibly empty signal only after a check, and Angular tracks it only when the check passes.
- No bookkeeping. You never update a dependency list by hand, so it cannot go out of date.
Short-circuit operators behave like the ternary above. In a() && b(), the signal b is a dependency only on runs where a() is truthy.
The same rule applies to effects
An effect tracks the signals it reads in the same way. Each run rebuilds its dependency set. This produces a common mistake:
import { Component, signal, effect } from '@angular/core';
@Component({
selector: 'app-feed',
template: ``,
})
export class FeedComponent {
enabled = signal(false);
items = signal<string[]>([]);
constructor() {
effect(() => {
if (!this.enabled()) {
return;
}
console.log('items:', this.items());
});
}
}On the first run, enabled() is false, so the effect returns early. It never reads items, so only enabled is a dependency. Changing items does nothing. After enabled becomes true, the effect re-runs, reads items, and tracks both signals from then on.
This is usually what you want. It becomes a bug when you expect the effect to react to a signal that the current run never reached.
Debugging checklist
When a derived value does not update as expected, work through these questions:
- Was the signal read during the latest evaluation, or did a branch skip it?
- Is the signal read through a function call, such as
this.page(), and not passed as the signal object itself? Without the call, no read happens and nothing is tracked. - Does the value only change after the computed is read again? Computed values are lazy, so a stale value is recalculated at the next read.
- Does the read happen inside
untracked()? Reads there are deliberately excluded. The next chapters cover this.
The second point is easy to miss. In computed(() => cond ? other : 0), the expression cond is a signal object, which is always truthy, and reading it does not register a dependency. The correct form is cond().
Summary of the mechanism
| Situation during a run | Tracked? |
|---|---|
| Signal called with `()` on the executed path | Yes |
| Signal in a branch that did not execute | No |
| Signal referenced but not called | No |
| Signal read earlier than a thrown error | Yes, up to the error |
Exercise
A computed is defined as const view = computed(() => user() ? profile() : 'guest');. At first user() returns null. Which signals are dependencies of view after the first evaluation, and what happens when profile.set(...) is called?
Show answer
Only user is a dependency, because profile() is never called on the null path. Calling profile.set(...) does not invalidate view, so the cached 'guest' stays. After user changes to a truthy value, view is stale. The next read re-runs it, reads profile(), and from then on tracks both user and profile.