Repository navigation
Conversation
`NgForm.addControl` and `NgForm.addFormGroup` register their directive in
a microtask. If the parent `ngModelGroup` is destroyed in the same tick in
which the control is added, `_findContainer` returns `null` by the time the
microtask runs, and calling `registerControl` on it throws:
TypeError: Cannot read properties of null (reading 'registerControl')
Guard the deferred registration against a missing container, matching the
already null-safe behavior of `removeControl` and `removeFormGroup`. Also
update the return type of `_findContainer` to `FormGroup | null` so that the
nullable case is reflected in the type system.
Fixes angular#69440
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #69440
NgForm.addControlandNgForm.addFormGroupdefer registration to a microtask. If the parentngModelGroupis destroyed in the same tick in which the control is added,_findContainer()returnsnullby the time the microtask runs, andcontainer.registerControl(...)throws:The matching
removeControl/removeFormGroupmethods are already null-safe, so the add path was inconsistent.What is the new behavior?
The deferred registration now returns early when the parent container no longer exists, instead of throwing. The return type of
_findContainer()is also corrected toFormGroup | nullso the nullable case is visible to the type system.Regression tests cover both
addControlandaddFormGroup, asserting that no global error/unhandled rejection is produced when the container is gone.Does this PR introduce a breaking change?
Other information
Related to #69440 and the earlier stalled attempt in #69543.