NG.VII · Angular 22 practice question
A component has a required signal input userId and wants to automatically re-fetch user data whenever userId changes. Which definition uses httpResource correctly?
Check answer and explanation
Correct answer: user = httpResource(() => `/api/user/${this.userId()}`);
Explanation
Passing a reactive function that returns the URL string, such as user = httpResource(() => `/api/user/${this.userId()}`), allows httpResource to track the signal and re-fetch on updates.
Common trap
Evaluating the template string eagerly passes a static string instead of a reactive function, so changes to userId would not trigger new requests. httpResource does not accept a signal as its first argument and a base URL string as its second argument.
Source: Angular docs: Reactive data fetching with `httpResource` — `Using httpResource`
Checked against the cited text on 2026-11-25.