1
1
import {
2
2
ComponentRef ,
3
3
Injector ,
4
- Injectable
4
+ Injectable ,
5
+ inject ,
6
+ EnvironmentInjector
5
7
} from "@angular/core" ;
6
8
import { PlaceholderService } from "carbon-components-angular/placeholder" ;
7
9
import { tap , delay } from "rxjs/operators" ;
@@ -16,6 +18,23 @@ export class BaseModalService {
16
18
// track all our open modals
17
19
protected static modalList : Array < ComponentRef < any > > = [ ] ;
18
20
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
+
19
38
/**
20
39
* Creates an instance of `ModalService`.
21
40
*/
@@ -34,7 +53,12 @@ export class BaseModalService {
34
53
useValue : data . inputs [ inputName ]
35
54
} ) ) ;
36
55
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
+ ) ;
38
62
let focusedElement = document . activeElement as HTMLElement ;
39
63
setTimeout ( ( ) => {
40
64
component . instance . open = true ;
0 commit comments