11import {
22 ComponentRef ,
33 Injector ,
4- Injectable
4+ Injectable ,
5+ inject ,
6+ EnvironmentInjector
57} from "@angular/core" ;
68import { PlaceholderService } from "carbon-components-angular/placeholder" ;
79import { 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 ;
0 commit comments