Skip to content

Commit 2e34121

Browse files
authored
Merge pull request #3113 from Akshat55/modal-enviornment-injector
fix: pass EnvironmentInjector to provide current module services to modals
2 parents 9753705 + 707da90 commit 2e34121

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/modal/base-modal.service.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
ComponentRef,
33
Injector,
4-
Injectable
4+
Injectable,
5+
inject,
6+
EnvironmentInjector
57
} from "@angular/core";
68
import { PlaceholderService } from "carbon-components-angular/placeholder";
79
import { tap, delay } from "rxjs/operators";
@@ -16,6 +18,23 @@ export class BaseModalService {
1618
// track all our open modals
1719
protected static modalList: Array<ComponentRef<any>> = [];
1820

21+
/**
22+
* Current module/component injection enviornment
23+
* Allows modules to use providers from calling component
24+
*
25+
* Root Module/
26+
* └── Lazy loaded Feature Module/
27+
* ├── Provides Service & imports modules
28+
* ├── Modal component (component that extends base component)
29+
* └── Modal component launcher (dynamically creates modal component)
30+
*
31+
* Passing EnvironmentInjector in `createComponent` will look for provider declaration in feature
32+
* module instead of root module. This is required to pass correct context in a lazy-loaded applications.
33+
* Services injected in root, will also be available as feature module enviornment will also hierarchically inherit
34+
* the root services.
35+
*/
36+
protected environment: EnvironmentInjector = inject(EnvironmentInjector);
37+
1938
/**
2039
* Creates an instance of `ModalService`.
2140
*/
@@ -34,7 +53,12 @@ export class BaseModalService {
3453
useValue: data.inputs[inputName]
3554
}));
3655
const injector = Injector.create({ providers: inputProviders });
37-
const component = this.placeholderService.createComponent(data.component, injector);
56+
const component = this.placeholderService.createComponent(
57+
data.component,
58+
injector,
59+
undefined,
60+
this.environment
61+
);
3862
let focusedElement = document.activeElement as HTMLElement;
3963
setTimeout(() => {
4064
component.instance.open = true;

src/placeholder/placeholder.service.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
ComponentRef,
33
ViewContainerRef,
4-
Injector
4+
Injector,
5+
EnvironmentInjector,
6+
inject
57
} from "@angular/core";
68
import { Injectable } from "@angular/core";
79

@@ -33,7 +35,12 @@ export class PlaceholderService {
3335
/**
3436
* Creates and returns component in the view.
3537
*/
36-
createComponent(component: ComponentRef<any>, injector: Injector, id?: any): ComponentRef<any> {
38+
createComponent(
39+
component: ComponentRef<any>,
40+
injector: Injector,
41+
id?: any,
42+
environment: EnvironmentInjector = undefined
43+
): ComponentRef<any> {
3744
if (id) {
3845
if (!this.viewContainerMap.has(id)) {
3946
console.error(`No view container with id ${id} found`);
@@ -45,7 +52,13 @@ export class PlaceholderService {
4552
console.error("No view container defined! Likely due to a missing `cds-placeholder`");
4653
return;
4754
}
48-
return this.viewContainerRef.createComponent(component as any, { index: this.viewContainerRef.length, injector });
55+
return this.viewContainerRef.createComponent(component as any,
56+
{
57+
index: this.viewContainerRef.length,
58+
injector,
59+
environmentInjector: environment
60+
}
61+
);
4962
}
5063

5164
destroyComponent(component: ComponentRef<any>) {

0 commit comments

Comments
 (0)