Skip to content

Commit f1d390f

Browse files
fix(hooks): Use an APP_INITIALIZER to sync/listen to the URL
Do not sync in StateService initializer Closes #132
1 parent 0943ece commit f1d390f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/providers.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
* @preferred @module ng2
8686
*/
8787
/** */
88-
import { Injector, Provider, PLATFORM_ID } from "@angular/core";
88+
import { Injector, Provider, PLATFORM_ID, APP_INITIALIZER } from "@angular/core";
8989
import { isPlatformBrowser } from "@angular/common";
9090
import {
9191
UIRouter, PathNode, StateRegistry, StateService, TransitionService, UrlMatcherFactory, UrlRouter, ViewService,
@@ -154,20 +154,25 @@ export function uiRouterFactory(locationStrategy: LocationStrategy, rootModules:
154154
rootModules.forEach(moduleConfig => applyRootModuleConfig(router, injector, moduleConfig));
155155
modules.forEach(moduleConfig => applyModuleConfig(router, injector, moduleConfig));
156156

157-
// Start monitoring the URL
158-
if (!router.urlRouter.interceptDeferred) {
159-
router.urlService.listen();
160-
router.urlService.sync();
161-
}
162-
163157
return router;
164158
}
165159

160+
// Start monitoring the URL when the app starts
161+
export function appInitializer(router: UIRouter) {
162+
return () => {
163+
if (!router.urlRouter.interceptDeferred) {
164+
router.urlService.listen();
165+
router.urlService.sync();
166+
}
167+
}
168+
}
169+
166170
export function parentUIViewInjectFactory(r: StateRegistry) { return { fqn: null, context: r.root() } as ParentUIViewInject; }
167171

168172
export const _UIROUTER_INSTANCE_PROVIDERS: Provider[] = [
169173
{ provide: UIRouter, useFactory: uiRouterFactory, deps: [LocationStrategy, UIROUTER_ROOT_MODULE, UIROUTER_MODULE_TOKEN, Injector] },
170174
{ provide: UIView.PARENT_INJECT, useFactory: parentUIViewInjectFactory, deps: [StateRegistry]},
175+
{ provide: APP_INITIALIZER, useFactory: appInitializer, deps: [UIRouter], multi: true },
171176
];
172177

173178
export function fnStateService(r: UIRouter) { return r.stateService; }

0 commit comments

Comments
 (0)