You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As written, the example rewrote the input value as the user edited it,
causing all kinds of confusing behavior which rendered the input nearly
unusable. While such implementations are possible, they require great
care to manage the cursor position while editing. For the sake of
brevity, this example now only updates the model on `blur` events; once
the user is done editing.
Copy file name to clipboardExpand all lines: adev/src/content/guide/forms/signals/custom-controls.md
+21-10Lines changed: 21 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -353,10 +353,10 @@ Most state properties use `input()` (read-only from the form). Use `model()` for
353
353
354
354
Controls sometimes display values differently than the form model stores them - a date picker might display "January 15, 2024" while storing "2024-01-15", or a currency input might show "$1,234.56" while storing 1234.56.
355
355
356
-
Use `computed()` signals (from `@angular/core`) to transform the model value for display, and handle input events to parse user input back to the storage format:
356
+
Use `linkedSignal()` (from `@angular/core`) to transform the model value for display, and handle input events to parse user input back to the storage format:
357
357
358
358
```angular-ts
359
-
import { Component, model, computed, ChangeDetectionStrategy } from '@angular/core';
359
+
import { ChangeDetectionStrategy, Component, linkedSignal, model } from '@angular/core';
360
360
import { FormValueControl } from '@angular/forms/signals';
361
361
362
362
@Component({
@@ -365,23 +365,34 @@ import { FormValueControl } from '@angular/forms/signals';
365
365
<input
366
366
type="text"
367
367
[value]="displayValue()"
368
-
(input)="handleInput($event.target.value)"
368
+
(input)="displayValue.set($event.target.value)"
369
+
(blur)="updateModel()"
369
370
/>
370
371
`,
371
372
changeDetection: ChangeDetectionStrategy.OnPush,
372
373
})
373
374
export class CurrencyInput implements FormValueControl<number> {
374
-
value = model<number>(0); // Stores numeric value (1234.56)
0 commit comments