Skip to content

Commit ebce639

Browse files
fix(lazyLoad): Supply native injector to lazy loaded states
Closes #35 Related to #44
1 parent 93c5138 commit ebce639

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/lazyLoad/lazyLoadNgModule.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/** @ng2api @module core */
22
/** */
33
import { NgModuleRef, Injector, NgModuleFactory, Type, Compiler, NgModuleFactoryLoader } from "@angular/core";
4-
import { Transition, LazyLoadResult, UIRouter, Resolvable, NATIVE_INJECTOR_TOKEN, isString } from "ui-router-core";
4+
import {
5+
Transition, LazyLoadResult, UIRouter, Resolvable, NATIVE_INJECTOR_TOKEN, isString, unnestR, inArray
6+
} from "ui-router-core";
57
import { RootModule, UIROUTER_ROOT_MODULE, UIROUTER_MODULE_TOKEN } from "../uiRouterNgModule";
68
import { applyModuleConfig } from "../uiRouterConfig";
79

@@ -159,15 +161,18 @@ export function applyNgModule(transition: Transition, ng2Module: NgModuleRef<any
159161
`using UIRouterModule.forChild({ states: CHILD_STATES }).`);
160162
}
161163

162-
// Supply the newly loaded states with the Injector from the lazy loaded NgModule
163-
replacementState.$$state().resolvables.push(Resolvable.fromData(NATIVE_INJECTOR_TOKEN, injector));
164-
newChildModules.forEach(module => {
165-
(module.states || []).forEach(state => {
166-
if (state.name && state.name.indexOf(replacementName) !== 0) {
167-
registry.get(state.name).$$state().resolvables.push(Resolvable.fromData(NATIVE_INJECTOR_TOKEN, injector));
168-
}
169-
});
170-
});
164+
// Supply the newly loaded states with the Injector from the lazy loaded NgModule.
165+
// If a tree of states is lazy loaded, only add the injector to the root of the lazy loaded tree.
166+
// The children will get the injector by resolve inheritance.
167+
let newStates = newChildModules.map(module => module.states)
168+
.reduce(unnestR, [])
169+
.map(state => state.$$state());
170+
171+
let newParentStates = newStates.filter(state => !inArray(newStates, state.parent));
172+
173+
// Add the Injector to the top of the lazy loaded state tree as a resolve
174+
newParentStates.forEach(state => state.resolvables.push(Resolvable.fromData(NATIVE_INJECTOR_TOKEN, injector)));
175+
171176

172177
return {};
173178
}

0 commit comments

Comments
 (0)