feat(forms): add support for standalone ngModel dirs inside forms #9522
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.
This PR allows the use of ngModels within forms without necessarily requiring the registration of their form controls with the parent form.
r: @vsavkin
cc: @StephenFluin: can you look at the new error message?
Before
If you wanted to include an ngModel within a form tag, it had to have a
name
attribute so that the ngModel could be registered with the parent form properly (for aggregate form validation and value).However, in some rare cases, it might be desirable to have an ngModel within a form tag, but not registered with the form. This setup was not supported and would throw an error.
After
Now, if for some reason you need to have a standalone ngModel within a form tag, it's still usable without registering it with the parent form by setting the
standalone
property inngModelOptions
. This way, you can explicitly dictate that you don't want the form registration, but users who may forget the name attribute will still get a helpful error.It's worth noting that ngModels can still, as always, be used outside of a form tag without a name attribute or an ngModelOptions property.