Sub topic 3
Querying the Template with Signal Queries
This chapter assumes you already know writable signals, computed(), and effect(), and that you have basic experience with RxJS Observables.
Signal Queries
viewChild(), viewChildren(), contentChild(), and contentChildren() each return a signal. Angular updates that signal when the matched elements change. Use viewChild.required() when the element is always present. Without it, the signal's type includes undefined.
import { Component, ElementRef, afterNextRender, viewChild } from '@angular/core';
@Component({
selector: 'app-name-field',
template: `<input #nameInput placeholder="Name" />`,
})
export class NameFieldComponent {
nameInput = viewChild.required<ElementRef<HTMLInputElement>>('nameInput');
constructor() {
afterNextRender(() => this.nameInput().nativeElement.focus());
}
}A common error is reading a required query in the constructor. The view does not exist yet at that point, so Angular throws an error.
Equality and untracked()
By default, a signal compares its old and new values with Object.is. You can pass { equal: (a, b) => a.id === b.id } to skip updates that you don't care about. Inside a computed() or an effect(), wrap a read in untracked(() => other()) when you need the value but don't want a change to it to rerun the code.
linkedSignal()
linkedSignal(() => sizes()[0]) gives you a writable signal that the user can set(). When the source signal (sizes) changes, the linked signal recomputes from it and the user's override is dropped.
Bridging Signals and RxJS
| Function | Direction | Notes |
|---|---|---|
| `toSignal(obs$, { initialValue })` | Observable → signal | Subscribes immediately and unsubscribes when the component is destroyed |
| `toObservable(sig)` | Signal → Observable | Emits from an effect, so values are delivered asynchronously |
You must call both functions in an injection context, such as a field initializer or the constructor.
Signal-Based Services
To share state, keep a private writable signal in the service and expose it with asReadonly(), along with computed() values derived from it. Only the service's methods can change the state. Replace arrays and objects instead of mutating them. A mutated array keeps the same reference, so the Object.is check sees no change and nothing updates.
resource() (Experimental)
resource() and httpResource() load async data whenever the signals they read change. They expose value(), isLoading(), and error() as signals. The API is still experimental, so check the version notes before you rely on it in production.
Exercise
A cart service exposes items as a read-only signal. A developer calls this._items().push(newItem) and the total never updates. Why, and what is the fix?
Show answer
push() changes the existing array, so the reference stays the same and the default Object.is equality sees no change. Consumers are never notified. Replace the array with this._items.update(items => [...items, newItem]).
Exercise
An effect logs the current user and also reads a visit counter, but it should rerun only when the user changes. What do you use?
Show answer
Read the counter inside untracked(() => this.visits()) so the effect does not track it as a dependency.