
Signal Inputs Are Read-Only: When to Reach for model() and linkedSignal()
- كاتب موثّق
- Angular 22
محتويات المقال
@Input() quantity = 1 to quantity = input(1). The old increment() method did this.quantity++, and its new version, this.quantity.set(...), breaks the build: property set doesn't exist on type InputSignal<number>. Someone suggests a quick fix, count = signal(this.quantity()). It compiles, and the + button works. Then QA files two bugs: the cart passes 3 and the stepper shows 1, and clicking + never changes the cart total.
The common misconception
input() returns a signal, signals have set(), so a component can update its own input when the user edits it, the same way it updates any other state.
signal(), by calling it, and you use it in computed() and effect() the same way. Decorator inputs built the habit: an @Input() was a plain field, so this.quantity++ compiled and the number on screen changed. But that write never reached the parent, and the next time the parent's bound value changed, Angular overwrote it without a word. Decorator inputs always followed the same rule as signal inputs. Signal inputs just put it in the type.
What actually happens
An input is the parent's value, delivered
<app-quantity-stepper [quantity]="qty()" />, Angular evaluates the binding, writes the result into the child's input, and does it again each time the bound value changes. The InputSignal the child receives is a read-only window on that delivery: it always holds the latest value the parent bound, and it has no set() or update(). If the child could write it, the value would have two writers and no rule for who wins. The parent would keep showing its own number, and its next update would silently erase the child's. One value with one owner is the rule, and the type enforces it.
The parent owns it and needs every change: use model().The child keeps a temporary copy that starts over when the parent sends a new value: use linkedSignal().Nobody edits it, you only need a different shape: use computed()or an input transform.
model(): an input that writes back
Change appended, and emits it every time the component calls set() or update(). When the parent binds with the two-way syntax, [(quantity)]="qty", Angular feeds the parent's value in and writes every emitted value back into the parent's signal or property. The parent still owns the number; the child is allowed to change it through the binding.
import { Component, input } from '@angular/core';@Component({ selector: 'app-quantity-stepper', template: ` <button (click)="increment()">+</button> <span>{{ quantity() }}</span> `,})export class QuantityStepper { readonly quantity = input(1); increment(): void { // TS2339: Property 'set' does not exist on type 'InputSignal<number>' this.quantity.set(this.quantity() + 1); }}import { Component, model } from '@angular/core';@Component({ selector: 'app-quantity-stepper', template: ` <button (click)="increment()">+</button> <span>{{ quantity() }}</span> `,})export class QuantityStepper { // An input plus a quantityChange output readonly quantity = model(1); increment(): void { this.quantity.update((q) => q + 1); // emits quantityChange }}qty = signal(3) in the cart and <app-quantity-stepper [(quantity)]="qty" /> in its template, the stepper starts at 3, each click updates the cart's qty, and the total that reads qty() follows. Notice that the two-way binding passes the signal itself, qty, not its value. Angular's docs recommend model inputs when a component exists to modify a value based on user interaction, most commonly custom form controls like a date picker or a combobox.
linkedSignal(): a local draft that follows the input
signal(this.name()) gets that wrong twice: it copies one value at construction, before the parent's binding is applied (an input.required() even throws NG0950 there), and it never looks at the input again. linkedSignal() gets both parts right:
import { Component, input, linkedSignal, output } from '@angular/core';@Component({ selector: 'app-rename-field', template: ` <input #box [value]="draft()" (input)="draft.set(box.value)" /> <button (click)="renamed.emit(draft())">Save</button> `,})export class RenameField { readonly name = input.required<string>(); readonly renamed = output<string>(); // Writable, and reset to the new name whenever the parent sends one readonly draft = linkedSignal(() => this.name());}draft is writable like any signal, so each keystroke calls draft.set(). Its computation is lazy and tracked like a computed(), so it first reads name() when the template renders, after the input is set. When the parent passes a new name, the next read of draft returns that name and the old draft is gone. Nothing reaches the parent until renamed.emit().
Transforms and computed(): when nobody writes
"5" for a number belongs in an input transform, like input(0, { transform: numberAttribute }), which runs each time Angular sets the input. A value derived for display, like quantity() * price(), belongs in computed(). Neither needs a writable signal. One limit to know: model inputs don't support transforms.
input() + computed() | |||
model() + [(x)] | |||
linkedSignal() | |||
signal(this.x()) |
How to apply it in your project
Find every write to an input With signal inputs, the compiler finds them for you. With @Input()fields, search each component for assignments to its own inputs, likethis.quantity =orthis.quantity++.Ask who owns the edit If the parent should see each change, switch the input to model()and bind it with[(x)]in the parent.Turn local edits into a linkedSignal() For drafts, selections and other state that should reset when the input changes, use linkedSignal()and keep reporting the final value through anoutput().Delete one-time copies Replace every signal(this.someInput())initializer withlinkedSignal()if the child writes it, orcomputed()if it doesn't.
Key takeaways
An input()is the parent's value: Angular writes it from the binding, and the component only reads it.model()is a writable input with an implicitChangeoutput; with[(x)], each write lands in the parent's state.linkedSignal()gives the child a writable copy that resets whenever the input changes.signal(this.input())freezes one early value, so it's almost always a bug.
التعليقات (0)