3
3
import { NgModuleRef , Injector , NgModuleFactory , Type , Compiler , NgModuleFactoryLoader } from "@angular/core" ;
4
4
import {
5
5
Transition , LazyLoadResult , UIRouter , Resolvable , NATIVE_INJECTOR_TOKEN , isString , unnestR , inArray , StateObject ,
6
- uniqR
6
+ uniqR , StateDeclaration
7
7
} from "@uirouter/core" ;
8
8
import { RootModule , UIROUTER_ROOT_MODULE , UIROUTER_MODULE_TOKEN , StatesModule } from "../uiRouterNgModule" ;
9
9
import { applyModuleConfig } from "../uiRouterConfig" ;
@@ -71,15 +71,15 @@ export type NgModuleToLoad = string | ModuleTypeCallback;
71
71
* - Finds the "replacement state" for the target state, and adds the new NgModule Injector to it (as a resolve)
72
72
* - Returns the new states array
73
73
*/
74
- export function loadNgModule ( moduleToLoad : NgModuleToLoad ) : ( transition : Transition ) => Promise < LazyLoadResult > {
75
- return ( transition : Transition ) => {
74
+ export function loadNgModule ( moduleToLoad : NgModuleToLoad ) : ( transition : Transition , stateObject : StateDeclaration ) => Promise < LazyLoadResult > {
75
+ return ( transition : Transition , stateObject : StateDeclaration ) => {
76
76
const ng2Injector = transition . injector ( ) . get ( NATIVE_INJECTOR_TOKEN ) ;
77
77
78
78
const createModule = ( factory : NgModuleFactory < any > ) =>
79
79
factory . create ( ng2Injector ) ;
80
80
81
81
const applyModule = ( moduleRef : NgModuleRef < any > ) =>
82
- applyNgModule ( transition , moduleRef ) ;
82
+ applyNgModule ( transition , moduleRef , ng2Injector , stateObject ) ;
83
83
84
84
return loadModuleFactory ( moduleToLoad , ng2Injector )
85
85
. then ( createModule )
@@ -131,13 +131,12 @@ export function loadModuleFactory(moduleToLoad: NgModuleToLoad, ng2Injector: Inj
131
131
*
132
132
* @internalapi
133
133
*/
134
- export function applyNgModule ( transition : Transition , ng2Module : NgModuleRef < any > ) : LazyLoadResult {
134
+ export function applyNgModule ( transition : Transition , ng2Module : NgModuleRef < any > , parentInjector : Injector , lazyLoadState : StateDeclaration ) : LazyLoadResult {
135
135
let injector = ng2Module . injector ;
136
- let parentInjector = < Injector > ng2Module . injector [ 'parent' ] || ng2Module . injector [ '_parent' ] ;
137
136
let uiRouter : UIRouter = injector . get ( UIRouter ) ;
138
137
let registry = uiRouter . stateRegistry ;
139
138
140
- let originalName = transition . to ( ) . name ;
139
+ let originalName = lazyLoadState . name ;
141
140
let originalState = registry . get ( originalName ) ;
142
141
// Check if it's a future state (ends with .**)
143
142
let isFuture = / ^ ( .* ) \. \* \* $ / . exec ( originalName ) ;
@@ -159,13 +158,15 @@ export function applyNgModule(transition: Transition, ng2Module: NgModuleRef<any
159
158
. reduce ( unnestR , [ ] )
160
159
. reduce ( uniqR , [ ] ) ;
161
160
162
- let replacementState = registry . get ( replacementName ) ;
163
- if ( ! replacementState || replacementState === originalState ) {
164
- throw new Error ( `The Future State named '${ originalName } ' lazy loaded an NgModule. ` +
165
- `The lazy loaded NgModule must have a state named '${ replacementName } ' ` +
166
- `which replaces the (placeholder) '${ originalName } ' Future State. ` +
167
- `Add a '${ replacementName } ' state to the lazy loaded NgModule ` +
168
- `using UIRouterModule.forChild({ states: CHILD_STATES }).` ) ;
161
+ if ( isFuture ) {
162
+ let replacementState = registry . get ( replacementName ) ;
163
+ if ( ! replacementState || replacementState === originalState ) {
164
+ throw new Error ( `The Future State named '${ originalName } ' lazy loaded an NgModule. ` +
165
+ `The lazy loaded NgModule must have a state named '${ replacementName } ' ` +
166
+ `which replaces the (placeholder) '${ originalName } ' Future State. ` +
167
+ `Add a '${ replacementName } ' state to the lazy loaded NgModule ` +
168
+ `using UIRouterModule.forChild({ states: CHILD_STATES }).` ) ;
169
+ }
169
170
}
170
171
171
172
// Supply the newly loaded states with the Injector from the lazy loaded NgModule.
0 commit comments