NG.II · Angular 22 practice question
A developer is implementing a state management pattern where a derived signal must maintain a user's selection even if the underlying list of options changes. Which approach correctly utilizes `linkedSignal` to achieve this preservation logic?
import { Component, signal, linkedSignal } from '@angular/core';
@Component({
selector: 'app-picker',
standalone: true,
template: '<!-- Template logic here -->'
})
export class PickerComponent {
shippingOptions = signal([{id: 0, name: 'Ground'}, {id: 1, name: 'Air'}] as {id: number, name: string}[]);
selectedOption = linkedSignal<{id: number, name: string}, {id: number, name: string}>({
source: this.shippingOptions,
computation: (newOptions, previous) => {
if (previous?.value) {
return newOptions.find(opt => opt.id === previous.value.id) ?? newOptions[0];
}
return newOptions[0];
},
});
}
Check answer and explanation
Correct answer: Defining the `linkedSignal` with a separate `source` and `computation` allows the function to access the previous state of the derived signal via the `previous` argument.
Explanation
The correct approach involves setting up the `linkedSignal` by providing distinct definitions for the `source` and the `computation`. This design enables the function to retrieve the prior state of the derived signal through the `previous` argument.
Common trap
The suggestion that the `linkedSignal` maintains its prior value without the `source` signal changing, thereby bypassing the `computation` function, is flawed. Furthermore, the claim that the `computation` function only receives the new `source` value, precluding a comparison with the prior selection, is inaccurate because the `previous` object furnishes access to the former linked signal value.
Source: Angular docs: Dependent state with `linkedSignal` — Accounting for previous state
Checked against the cited text on 2026-11-25.