Description
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
We rely on _onCollectionChange()
to notify its FormGroupDirective to re-bind its FormControlName directives, and FormGroup is smart enough to propagate its _onCollectionChange()
callback so that Child FormGroups can notify too. This happens in two scenarios
- When we first initialize the FormGroup (the constructor calls
_setupControl()
which binds_onCollectionChange()
to the children - When we call
registerControl
(and associated methods)
The problem is with scenario 1: we may seed each child control with a default callback (an empty function) on init, but we don't propagate a new callback when we eventually bind the FormGroup to a FormGroupDirective. The child FormGroups are still working with the 'default' one. Furthermore the api is private so I don't see any way to fix this on the user side.
If we have a scenario where a child form group adds controls dynamically, and some of those controls are FormControlName directives (see stackblitz), then we fail to trigger _updateDOMValue()
on the parent FormGroupDirective. The FormControlName directives are left with orphaned form controls.
Ideally, _registerOnCollectionChange()
would cascade to its children (or maybe just call _setupControls()
again).
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/stackblitz-starters-iatn8h?file=src%2Fbroken-form.ts
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run ng version
)
Angular CLI: 18.2.0
Node: 20.11.1
Package Manager: yarn 4.4.0
OS: linux x64
Angular: 18.2.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, language-service, material, platform-browser
... platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1802.0
@angular-devkit/build-angular 18.2.0
@angular-devkit/core 18.2.0
@angular-devkit/schematics 18.2.0
@angular/flex-layout 14.0.0-beta.41
@schematics/angular 18.2.0
ng-packagr 18.2.0
rxjs 7.8.1
typescript 5.4.5
zone.js 0.14.10
Anything else?
No response