Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
A FormValueControl that uses transformedValue shows an empty input after a reactive form is reset and then set back to the value it had before the reset, in the same tick.
import { ChangeDetectionStrategy, Component, model } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { FormValueControl, transformedValue } from '@angular/forms/signals';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'app-text-input',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<input [value]="rawValue()" (input)="rawValue.set($any($event.target).value)" />`,
})
export class TextInput implements FormValueControl<string | null> {
readonly value = model<string | null>(null);
protected readonly rawValue = transformedValue(this.value, {
parse: (raw: string) => ({ value: raw || null }),
format: (value: string | null) => value ?? '',
});
}
@Component({
selector: 'app-root',
imports: [ReactiveFormsModule, TextInput],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<form [formGroup]="form">
<app-text-input formControlName="name" />
<input formControlName="name" />
</form>
<button (click)="reload()">reset + patchValue (same value)</button>
<p>Control value: "{{ form.value.name }}"</p>
`,
})
export class App {
protected readonly form = new FormGroup({ name: new FormControl<string | null>('Ada') });
protected reload(): void {
this.form.reset();
this.form.patchValue({ name: 'Ada' });
}
}
bootstrapApplication(App);Click the button. The native input and the control value still show "Ada", but the custom input is empty. If change detection runs between reset() and patchValue(), everything stays in sync.
Cause
The reset propagation added in 849dba6 updates the raw value without updating the model or the cache of the last value written to the control:
- On
FormResetEvent,NgControlcalls the reset callback withcontrol.value, which isnullhere (ng_control.ts). transformedValuewritesformat(null)into its raw-valuelinkedSignalwithoriginalSet, bypassing the model on purpose (transformed_value.ts). The control'svaluemodel is still'Ada', becauseNgControlonly writes it during change detection.patchValue({name: 'Ada'})sets theFormControlback to'Ada'.- During change detection,
ngControlUpdateskips the write becausecustomControlBindings.valueis still'Ada'(ng_control.ts). The model never changes, so thelinkedSignalnever recomputes and keeps the empty raw value.
The same commit handles this for CVA controls under [formField]: the reset binding in control_cva.ts writes the value and updates bindings['controlValue'], so later updates are compared against what the control really holds. The FVC path under NgControl has no equivalent.
Proposed fix
Mirror the CVA reset in NgControl. On FormResetEvent, before calling the reset callback, NgControl should write the reset value to the custom control's value model and update customControlBindings.value to match. Any pending custom value (for updateOn: 'blur' or 'submit') should be discarded at the same time, as ngControlUpdate already does when it overwrites the control.
The model has to be written during the reset, not on the next change detection. If the write is deferred, transformedValue still formats the reset value while the model holds the old one, and when the previous value is written back the model doesn't change, so the raw value is never recomputed.
Please provide a link to a minimal reproduction of the bug
The example in the description is a complete src/main.ts: paste it into a new Angular 22 project (for example ng new --minimal) and run ng serve.
Please provide the exception or error you saw
No errors
Please provide the environment you discovered this bug in (run ng version)
Angular CLI : 22.2.7
Angular : 22.2.6
Node.js : 22.23.3
Package Manager : yarn 1.22.22
Operating System : win32 x64
┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build │ 22.2.7 │ 22.2.7 │
│ @angular/cli │ 22.2.7 │ 22.2.7 │
│ @angular/common │ 22.2.6 │ 22.2.6 │
│ @angular/compiler │ 22.2.6 │ 22.2.6 │
│ @angular/compiler-cli │ 22.2.6 │ 22.2.6 │
│ @angular/core │ 22.2.6 │ 22.2.6 │
│ @angular/forms │ 22.2.6 │ 22.2.6 │
│ @angular/platform-browser │ 22.2.6 │ 22.2.6 │
│ rxjs │ 7.8.2 │ 7.8.2 │
│ typescript │ 6.0.3 │ ~6.0.3 │
└───────────────────────────┴───────────────────┴───────────────────┘
Anything else?
#72065 looks related but is a different problem: there the control writes its own model under [formField], and the remembered value in control_custom.ts is not updated. Here the control's model is never written; only the raw value of transformedValue is.
Activity