|
1 | 1 | /** @ng2api @module core */
|
2 | 2 | /** */
|
3 | 3 | 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"; |
5 | 7 | import { RootModule, UIROUTER_ROOT_MODULE, UIROUTER_MODULE_TOKEN } from "../uiRouterNgModule";
|
6 | 8 | import { applyModuleConfig } from "../uiRouterConfig";
|
7 | 9 |
|
@@ -159,15 +161,18 @@ export function applyNgModule(transition: Transition, ng2Module: NgModuleRef<any
|
159 | 161 | `using UIRouterModule.forChild({ states: CHILD_STATES }).`);
|
160 | 162 | }
|
161 | 163 |
|
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 | + |
171 | 176 |
|
172 | 177 | return {};
|
173 | 178 | }
|
|
0 commit comments