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

bug(MatDialog): Tokens from lazy module are not available in another module's popup component #22021

Open
artaommahe opened this issue Feb 25, 2021 · 3 comments
Labels
area: material/dialog P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@artaommahe
Copy link

artaommahe commented Feb 25, 2021

Reproduction

https://stackblitz.com/edit/angular-material-dialog-token-from-lazy-module?file=src%2Fapp%2F%2Bteacher%2Fcomponents%2Fteacher-page%2Fteacher-page.ts

Steps to reproduce:

  1. go to /teacher route
  2. click open popup
  3. see an exception on ContentComponents token access inside HomeworkPopupComponent

Expected Behavior

MatDialog uses passed ViewContainerRef`s injector as a parent for popup component, so it's expected to have all tokens from such injector available inside popup

const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;

Actual Behavior

ERROR
Error: R3InjectorError(AppModule)[InjectionToken ContentComponents -> InjectionToken ContentComponents -> InjectionToken ContentComponents]:
NullInjectorError: No provider for InjectionToken ContentComponents!

ContentComponents token is available from TeacherPageComponent's viewContainerRef.injector.
Also it's available from @SkipSelf() private injector: Injector inside HomeworkPopupComponent. But we can't use this hack because we need this token deep in HomeworkPopupComponent's children.

Environment

  • Angular: 11.2.3
  • CDK/Material: 11.2.2
  • Browser(s): Chrome Version 88.0.4324.182
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows 10
@artaommahe artaommahe added the needs triage This issue needs to be triaged by the team label Feb 25, 2021
@mmalerba mmalerba added area: material/dialog P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed needs triage This issue needs to be triaged by the team labels Mar 1, 2021
@mmalerba
Copy link
Contributor

mmalerba commented Mar 1, 2021

Sharing injected data across lazy loaded modules is an issue that has come up for a number of our components. We should look into improving this experience either through code changes or better documentation. Some of the other issues discuss workarounds people have used that may be helpful here as well: #12891 #10084 #18970

@jelbourn jelbourn self-assigned this Mar 8, 2021
@LeraAl
Copy link

LeraAl commented Feb 9, 2023

Hello!
Have the same issue on our project with MatDialog.
Can not divide application to modules, viewContainerRef doesn't work as well.
Services, that are injected only to the lazy-loaded module can not be accessible from the Dialog.

@LeraAl
Copy link

LeraAl commented Feb 9, 2023

It seems I found the root cause of the problem.

Component is created with deprecated API.
So, it used the deprecated version of createComponent method with componentFactoryResolver.
https://github.com/angular/components/blob/main/src/cdk/portal/portal-directives.ts#L155

As a result, component is created with componentFactoryResolver connected to the AppModule if it's not explicitly provided.
Is it possible to fix the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/dialog P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
4 participants