@@ -10,6 +10,7 @@ import {ComponentRef, EnvironmentInjector, Injectable} from '@angular/core';
1010
1111import { RouterOutletContract } from './directives/router_outlet' ;
1212import { ActivatedRoute } from './router_state' ;
13+ import { getClosestRouteInjector } from './utils/config' ;
1314
1415/**
1516 * Store contextual information about a `RouterOutlet`
@@ -19,9 +20,15 @@ import {ActivatedRoute} from './router_state';
1920export class OutletContext {
2021 outlet : RouterOutletContract | null = null ;
2122 route : ActivatedRoute | null = null ;
22- children = new ChildrenOutletContexts ( this . injector ) ;
23+ children = new ChildrenOutletContexts ( this . rootInjector ) ;
2324 attachRef : ComponentRef < any > | null = null ;
24- constructor ( public injector : EnvironmentInjector ) { }
25+ get injector ( ) : EnvironmentInjector {
26+ return getClosestRouteInjector ( this . route ?. snapshot ) ?? this . rootInjector ;
27+ }
28+ // TODO(atscott): Only here to avoid a "breaking" change in a patch/minor. Remove in v19.
29+ set injector ( _ : EnvironmentInjector ) { }
30+
31+ constructor ( private readonly rootInjector : EnvironmentInjector ) { }
2532}
2633
2734/**
@@ -35,7 +42,7 @@ export class ChildrenOutletContexts {
3542 private contexts = new Map < string , OutletContext > ( ) ;
3643
3744 /** @nodoc */
38- constructor ( private parentInjector : EnvironmentInjector ) { }
45+ constructor ( private rootInjector : EnvironmentInjector ) { }
3946
4047 /** Called when a `RouterOutlet` directive is instantiated */
4148 onChildOutletCreated ( childName : string , outlet : RouterOutletContract ) : void {
@@ -75,7 +82,7 @@ export class ChildrenOutletContexts {
7582 let context = this . getContext ( childName ) ;
7683
7784 if ( ! context ) {
78- context = new OutletContext ( this . parentInjector ) ;
85+ context = new OutletContext ( this . rootInjector ) ;
7986 this . contexts . set ( childName , context ) ;
8087 }
8188
0 commit comments