-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Description
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
Consider the following example with a signal form of two independent fields. Of those, the first field's value is logged using an effect that is accessing this.form.first().value():
@Component({
selector: 'app-root',
imports: [Control],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<input [control]="form.first"/>
<input [control]="form.second"/>
`,
})
export class App {
readonly form = form(signal({ first: '', second: '' }));
readonly logFirstEffect = effect(() => {
console.log(this.form.first().value());
});
}Notice that when typing in the second control, the effect still fires. This is because this.form.first() is re-computed.
For me, this was initially unexpected, and caused an infinite effect, when trying to conditional sync form values, which is a feature you seem willing to provide a native solution for in [Design] fields whose value is sometimes computed from other fields.
One can, of course, circumvent this issue by memorising the value as
readonly firstValue = computed(() => this.form.first().value())As there is a general need to listen to individual field value changes in the spirit of valueChanges offered by reactive forms, be it via an effect or a computed signal, could you please consider on of the following:
- Optimize the value updates so that they are not triggered on unrelated changes.
- Expose field value changes via a different API that only updates when underlying value changes.
- Document this potential pitfall explicitly within your upcoming documentation.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/stackblitz-starters-46vthvkl?file=src%2Fmain.ts
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 21.0.0-next.2
Node: 20.19.1
Package Manager: npm 10.8.2
OS: linux x64
Angular: 21.0.0-next.2
... build, cli, common, compiler, compiler-cli, core, forms
... platform-browser, router
Package Version
------------------------------------
@angular-devkit/architect 0.2100.0-next.2
@angular-devkit/core 21.0.0-next.2
@angular-devkit/schematics 21.0.0-next.2
@schematics/angular 21.0.0-next.2
rxjs 7.8.2
typescript 5.9.2
Anything else?
This might just be an observation and possible area of optimisation that you already aware of and addressing as part of [Optimize] Value updates and recomputation of childrenMap.

