diff --git a/packages/core/src/application/application_ref.ts b/packages/core/src/application/application_ref.ts index 0edff39546c..7ef3493d26a 100644 --- a/packages/core/src/application/application_ref.ts +++ b/packages/core/src/application/application_ref.ts @@ -498,6 +498,14 @@ export class ApplicationRef { bootstrap( componentOrFactory: ComponentFactory | Type, rootSelectorOrNode?: string | any, + ): ComponentRef { + return this.bootstrapImpl(componentOrFactory, rootSelectorOrNode); + } + + private bootstrapImpl( + componentOrFactory: ComponentFactory | Type, + rootSelectorOrNode?: string | any, + injector: Injector = Injector.NULL, ): ComponentRef { profiler(ProfilerEvent.BootstrapComponentStart); @@ -532,7 +540,7 @@ export class ApplicationRef { ? undefined : this._injector.get(NgModuleRef); const selectorOrNode = rootSelectorOrNode || componentFactory.selector; - const compRef = componentFactory.create(Injector.NULL, [], selectorOrNode, ngModule); + const compRef = componentFactory.create(injector, [], selectorOrNode, ngModule); const nativeElement = compRef.location.nativeElement; const testability = compRef.injector.get(TESTABILITY, null); testability?.registerApplication(nativeElement); diff --git a/packages/core/test/application_ref_spec.ts b/packages/core/test/application_ref_spec.ts index d559774b24a..f96b1295d2a 100644 --- a/packages/core/test/application_ref_spec.ts +++ b/packages/core/test/application_ref_spec.ts @@ -256,6 +256,34 @@ describe('bootstrap', () => { ), ); }); + + describe('bootstrapImpl', () => { + it('should use a provided injector', inject([ApplicationRef], (ref: ApplicationRef) => { + class MyService {} + const myService = new MyService(); + + @Component({ + selector: 'injecting-component', + template: `
Hello, World!
`, + }) + class InjectingComponent { + constructor(readonly myService: MyService) {} + } + + const injector = Injector.create({ + providers: [{provide: MyService, useValue: myService}], + }); + + createRootEl('injecting-component'); + const appRef = ref as unknown as {bootstrapImpl: ApplicationRef['bootstrapImpl']}; + const compRef = appRef.bootstrapImpl( + InjectingComponent, + /* rootSelectorOrNode */ undefined, + injector, + ); + expect(compRef.instance.myService).toBe(myService); + })); + }); }); describe('destroy', () => {