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

Regression: modal components don't have the correct injector #4447

Closed
jnizet opened this issue Dec 22, 2022 · 8 comments · Fixed by #4464
Closed

Regression: modal components don't have the correct injector #4447

jnizet opened this issue Dec 22, 2022 · 8 comments · Fixed by #4464

Comments

@jnizet
Copy link
Member

jnizet commented Dec 22, 2022

Bug description:

Since version 14, a component used as the modal content doesn't have the correct injector anymore.

If a lazy loaded module defines a provider for a service and a component from that module is used as a modal content and tries to get the service via DI, a NullInjectorError is thrown when opening the modal.

Link to minimally-working StackBlitz that reproduces the issue:

https://stackblitz.com/github/jnizet/modal-repro/tree/ngb14

This repro is also available on github: https://github.com/jnizet/modal-repro

The main branch shows the code working with ng-bootstrap 13, and the ngb14 branch shows the same code not working with ng-bootstrap 14.

Versions of Angular, ng-bootstrap and Bootstrap:

Angular: 15.0.4

ng-bootstrap: 14.0.0

Bootstrap: 5.2.3

@stefanwaldhauser
Copy link

I have the same problem. Does somebody know of a hotfix for this problem?

@krusche
Copy link

krusche commented Dec 30, 2022

We have the same problem and would appreciate a fix

@egdw-nipra
Copy link

This really needs more attention. v.14.0.0 is currently completely broken for anyone using lazy loaded modules.

@maxokorokov maxokorokov added this to the 14.0.1 milestone Jan 5, 2023
@maxokorokov
Copy link
Member

maxokorokov commented Jan 5, 2023

Indeed, I think I've screwed up two things:

  1. Modal content creation

const elementInjector = Injector.create({
providers: [{ provide: NgbActiveModal, useValue: context }],
parent: contentInjector,
});
const componentRef = createComponent(componentType, {
environmentInjector: this._applicationRef.injector,
elementInjector,
});

I think environmentInjector should be contentInjector.get(EnvironmentInjector, null) || this._applicationRef.injector.

  1. NgbModalModule

@NgModule({})
export class NgbModalModule {}

It should provide NgbModal, so @NgModule({providers: [NgbModal]})

I'll open a PR ASAP

@jnizet
Copy link
Member Author

jnizet commented Jan 5, 2023

If NgbModal needs to be provided by NgbModalModule for this bug to be fixed, does it mean that standalone components should have it in their providers if they don't import the NgbModalModule? If so, it would be worth documenting it.

@jcalijurio
Copy link

I have the same problem here too.

maxokorokov added a commit to maxokorokov/ng-bootstrap that referenced this issue Jan 9, 2023
Provides `NgbModal` at the `NgbModalModule` level as well as at the 'root' level.
Provides environment injector based on the `contentInjector` and not the `appRef.injector` when using component as modal content.

Fixes ng-bootstrap#4447
@maxokorokov
Copy link
Member

@jnizet I've opened the PR
I think we should simply provide NgbModal both in NgbModalModule and in 'root'. I didn't manage to reproduce this issue with standalone components

maxokorokov added a commit that referenced this issue Jan 9, 2023
Provides `NgbModal` at the `NgbModalModule` level as well as at the 'root' level.
Provides environment injector based on the `contentInjector` and not the `appRef.injector` when using component as modal content.

Fixes #4447
@jnizet
Copy link
Member Author

jnizet commented Jan 9, 2023

Thanks for the fix and for the release @maxokorokov. Works fine here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants