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

Can't inject environmentInjector into dialog opened by DialogService #14491

Closed
axos88 opened this issue Jan 5, 2024 · 9 comments
Closed

Can't inject environmentInjector into dialog opened by DialogService #14491

axos88 opened this issue Jan 5, 2024 · 9 comments
Labels
Status: Discussion Issue or pull request needs to be discussed by Core Team

Comments

@axos88
Copy link

axos88 commented Jan 5, 2024

Describe the bug

Opening a standalone dynamic dialog via the DialogService does not inherit the environment injector of the component making the call.
This leads to NullInjection issues if the opened standalone (maybe also non-standalone?) component uses providers that are NOT {providedIn: root}, but rather an intermediate module / component was providing them.

I've hacked around a solution locally, to fix the bug two things need to be made:

  1. extend the DynamicDialogConfig with an environmentInjector?: EnvironmentInjector field.
  2. In the DynamicDialogComponent in loadChildComponent method, replace the last line with:
    this.componentRef = viewContainerRef?.createComponent(componentType, {environmentInjector: this.config.environmentInjector });

Environment

angular 17

Reproducer

No response

Angular version

17

PrimeNG version

master

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

18

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

@axos88 axos88 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 5, 2024
@Draganlazarev90
Copy link

Did you manage to find a workaround for this except from changing the source code?

@cetincakiroglu cetincakiroglu added this to the 17.7.0 milestone Feb 12, 2024
@cetincakiroglu
Copy link
Contributor

Related to #14730

@cetincakiroglu cetincakiroglu added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Feb 12, 2024
@cetincakiroglu
Copy link
Contributor

cetincakiroglu commented Feb 14, 2024

Hi,

Your workaround solution seems to be correct, could you please open a PR so we can review the solution? Moving the issue to the 17.8.0 milestone while waiting the feedback from you.

@cetincakiroglu cetincakiroglu added the Resolution: Needs More Information More information about the issue is needed to find a correct solution label Feb 14, 2024
@cetincakiroglu cetincakiroglu modified the milestones: 17.7.0, 17.8.0 Feb 14, 2024
@Draganlazarev90
Copy link

The fix mentioned above requires sending the environment injector in the config, each time you use dialogService.open method. Maybe a different fix can be implemented

@cetincakiroglu
Copy link
Contributor

cetincakiroglu commented Feb 22, 2024

The strange thing is it works if I provide DemoService inside any parent component instead of module, see the example below. The component ignores the module providers somehow, we're investigating the solutions.

https://stackblitz.com/edit/7qkchf-expefk?file=src%2Fapp%2Fdemo%2Fdialog.component.ts,src%2Fapp%2Fdemo%2Fdemo.service.ts

@Draganlazarev90
Copy link

Correct, somehow it doesn't work only with modules

@cetincakiroglu
Copy link
Contributor

cetincakiroglu commented Feb 23, 2024

@Draganlazarev90

As a workaround, you can try to provide the service on the parent component rather than the module until we fix the issue. Somehow it skips the module providers, we're investigating the issue. It may work with standalone since there isn't a module and services are provided in the component.ts

I'll transfer the issue to the next milestone, we want to solve the issue without adding environmentInjector property to the config since it has to be done by the component itself. But in the opposite scenario, we may add it as a quick solution until we fix it for good.

@axos88 Can you confirm that it also doesn't work with standalone where you provide DemoService in parent component instead of the module?

@cetincakiroglu cetincakiroglu modified the milestones: 17.8.0, 17.9.0, 17.10.0 Feb 23, 2024
@cetincakiroglu
Copy link
Contributor

Hi,

After the investigation, we've found the issue caused by injection level difference. You're providing dialogService in the component level and demoService in the module level. Because of this, dialogService fails to find and inject DemoService. It works as expected if both of them are provided at the same level. That's why injecting the DemoService to the component.ts makes it work. Please see the example: https://stackblitz.com/edit/7qkchf-expefk?file=src%2Fapp%2Fdemo%2Fdemo.module.ts

To provide a better guide, we'll update the documentation of DynamicDialog and mention this use. Thanks a lot for reporting the issue!

I am closing the issue since the solution is provided.

@cetincakiroglu cetincakiroglu removed this from the 17.10.0 milestone Mar 6, 2024
@cetincakiroglu cetincakiroglu added Status: Discussion Issue or pull request needs to be discussed by Core Team and removed Type: Bug Issue contains a bug related to a specific component. Something about the component is not working Resolution: Needs More Information More information about the issue is needed to find a correct solution labels Mar 6, 2024
@abhijit-chikane
Copy link

abhijit-chikane commented Dec 6, 2024

image

I have provided the dependencies on the route level
and moving the dialogService to this provider doesn't solve this issue
so above mention workaround is not working for me
is there any other workaround for this scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Discussion Issue or pull request needs to be discussed by Core Team
Projects
None yet
Development

No branches or pull requests

4 participants