Skip to content

Commit 5b3fefa

Browse files
feat(components/core): add ability to provide a parent injector when constructing components via the SkyDynamicComponentService (#793)
1 parent ea50a7d commit 5b3fefa

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

libs/components/core/src/lib/modules/dynamic-component/dynamic-component-options.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StaticProvider } from '@angular/core';
1+
import { Injector, StaticProvider } from '@angular/core';
22

33
import { SkyDynamicComponentLocation } from './dynamic-component-location';
44

@@ -20,4 +20,9 @@ export interface SkyDynamicComponentOptions {
2020
* The reference element used when using the `ElementTop` or `ElementBottom` locations.
2121
*/
2222
referenceEl?: HTMLElement;
23+
24+
/**
25+
* Parent injector to use instead of the dynamic component service's injector
26+
*/
27+
parentInjector?: Injector;
2328
}

libs/components/core/src/lib/modules/dynamic-component/dynamic-component.service.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ApplicationRef,
33
ComponentRef,
44
EmbeddedViewRef,
5+
Injector,
56
StaticProvider,
67
} from '@angular/core';
78
import { TestBed, inject } from '@angular/core/testing';
@@ -18,7 +19,8 @@ describe('Dynamic component service', () => {
1819
function createTestComponent(
1920
location?: SkyDynamicComponentLocation,
2021
reference?: HTMLElement,
21-
providers?: StaticProvider[]
22+
providers?: StaticProvider[],
23+
injector?: Injector
2224
): ComponentRef<DynamicComponentTestComponent> {
2325
const svc: SkyDynamicComponentService = TestBed.inject(
2426
SkyDynamicComponentService
@@ -29,6 +31,7 @@ describe('Dynamic component service', () => {
2931
location: location,
3032
referenceEl: reference,
3133
providers,
34+
parentInjector: injector,
3235
});
3336

3437
cmpRef.changeDetectorRef.detectChanges();
@@ -160,6 +163,21 @@ describe('Dynamic component service', () => {
160163
);
161164
});
162165

166+
it('should use a parent injector if supplied', () => {
167+
const injector: Injector = Injector.create({
168+
providers: [{ provide: 'greeting', useValue: 'My name is Pat.' }],
169+
name: 'test injector',
170+
});
171+
createTestComponent(undefined, undefined, undefined, injector);
172+
173+
const el = getComponentEl(0);
174+
175+
expect(document.body.lastChild).toBe(el);
176+
expect(el.querySelector('.component-test')).toHaveText(
177+
'Hello world My name is Pat.'
178+
);
179+
});
180+
163181
it('should throw error if placing a component before an undefined element', () => {
164182
expect(() =>
165183
createTestComponent(

libs/components/core/src/lib/modules/dynamic-component/dynamic-component.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class SkyDynamicComponentService {
6464

6565
const injector = Injector.create({
6666
providers: options.providers || [],
67-
parent: this.#injector,
67+
parent: options.parentInjector || this.#injector,
6868
});
6969

7070
const componentRef = createComponent<T>(componentType, {

0 commit comments

Comments
 (0)