Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ngModel complains when used with @defer #55028

Closed
ssougnez opened this issue Mar 25, 2024 · 6 comments
Closed

ngModel complains when used with @defer #55028

ssougnez opened this issue Mar 25, 2024 · 6 comments
Assignees
Labels
area: core Issues related to the framework runtime core: defer Issues related to @defer blocks. P2 The issue is important to a large percentage of users, with a workaround
Milestone

Comments

@ssougnez
Copy link

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

Say we have a component with a form.
One of the field is a cutstom component implementing ControlValueAccessor.
This component uses ngModel without ngModelOptions (which might be a bad practice, but it works).
When the component hosting the form is used without @defer, it works as expected, but as soon as it is used in a @defer block, the error "ngModel cannot be used to register form controls with a parent formGroup directive." is thrown.

I'm not sure whether it's a bad practice or not to use ngModel without ngModelOptions in that scenario but I think the behavior should be consistent whether you use @defer or not. Either it should fail every time or works every time.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-17-starter-project-egsglm

Please provide the exception or error you saw

Simply remove the @defer/@placeholder from the stackblitz to enable/disable the error.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.2.3
Node: 20.11.1
Package Manager: yarn 1.22.19
OS: win32 x64

Angular: 17.2.3
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1702.2
@angular-devkit/build-angular   17.2.2
@angular-devkit/core            17.2.3
@angular-devkit/schematics      17.2.3
@schematics/angular             17.2.3
rxjs                            7.8.1
typescript                      5.2.2
zone.js                         0.14.2

Anything else?

No response

@JeanMeche JeanMeche added area: core Issues related to the framework runtime core: defer Issues related to @defer blocks. labels Mar 25, 2024
@ngbot ngbot bot added this to the needsTriage milestone Mar 25, 2024
@AndrewKushnir AndrewKushnir self-assigned this Mar 25, 2024
@AndrewKushnir
Copy link
Contributor

@ssougnez thanks for reporting the issue and providing a repro. The issue might be related to #54864 and we are working on a fix. While working on a fix for #54864, we can put together a test that would check Forms behavior as well. Will keep this thread updated once we make more progress.

@ssougnez
Copy link
Author

Okay thanks.

And just out of curiosity, is it a bad practice to have a component using ngModel for just one two-way binding without using ngModelOptions?

It never occurred to me so I was not doing it but now that I'm facing this issue, I'm wondering whether I should start using ngModelOptions in these cases.

@AndrewKushnir AndrewKushnir added the P2 The issue is important to a large percentage of users, with a workaround label Mar 26, 2024
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Mar 26, 2024
@AndrewKushnir
Copy link
Contributor

Quick update: I've confirmed that the root cause of this issue is the same as the one from #54864. We are working on a fix and will keep this thread updated once the fix is available.

And just out of curiosity, is it a bad practice to have a component using ngModel for just one two-way binding without using ngModelOptions?

If you do not rely on that <input> to act as a part of a larger form (and only used to sync a value between an input and a class field), you may consider using the ngModelOptions.standalone flag.

It never occurred to me so I was not doing it but now that I'm facing this issue, I'm wondering whether I should start using ngModelOptions in these cases.

The issue was caused by the bug in Angular's code, adding @defer should not cause the error in your scenario.

AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Mar 28, 2024
…ocks

This commit updates the `@defer` logic to establish proper injector resolution order. More specifically:

- Makes node injectors to be inspected first, similar to how it happens when `@defer` block is not used.
- Adds extra handling for the Router's `OutletInjector`, until we replace it with an `EnvironmentInjector`.

Resolves angular#54864.
Resolves angular#55028.
Resolves angular#55036.
dylhunn pushed a commit that referenced this issue Mar 28, 2024
…ocks (#55079)

This commit updates the `@defer` logic to establish proper injector resolution order. More specifically:

- Makes node injectors to be inspected first, similar to how it happens when `@defer` block is not used.
- Adds extra handling for the Router's `OutletInjector`, until we replace it with an `EnvironmentInjector`.

Resolves #54864.
Resolves #55028.
Resolves #55036.

PR Close #55079
@AndrewKushnir
Copy link
Contributor

Quick update: the fix for this issue was merged and released as a part of Angular v17.3.2 (available on NPM). Please let us know if you still see the issue after updating to v17.3.2.

@ssougnez
Copy link
Author

I already updated my code to use ngModelOptions, however, I tried this version on the failing stackblitz and I can reproduce the issue so I guess it is correctly fixed :-)

Thanks for the quick action.

ilirbeqirii pushed a commit to ilirbeqirii/angular that referenced this issue Apr 6, 2024
…ocks (angular#55079)

This commit updates the `@defer` logic to establish proper injector resolution order. More specifically:

- Makes node injectors to be inspected first, similar to how it happens when `@defer` block is not used.
- Adds extra handling for the Router's `OutletInjector`, until we replace it with an `EnvironmentInjector`.

Resolves angular#54864.
Resolves angular#55028.
Resolves angular#55036.

PR Close angular#55079
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: defer Issues related to @defer blocks. P2 The issue is important to a large percentage of users, with a workaround
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants