4646 *
4747 * @preferred @module ng2
4848 */ /** */
49- import { Injector , OpaqueToken } from "@angular/core" ;
49+ import { Injector , OpaqueToken , Provider } from "@angular/core" ;
50+ import { ClassProvider , ExistingProvider , FactoryProvider , TypeProvider , ValueProvider } from "@angular/core" ; // has or is using
5051import { UIRouter } from "../router" ;
5152import { PathNode } from "../path/node" ;
5253import { StateRegistry } from "../state/stateRegistry" ;
@@ -62,17 +63,20 @@ import {UIRouterConfig} from "./uiRouterConfig";
6263import { Globals } from "../globals" ;
6364import { UIRouterLocation } from "./location" ;
6465import { services } from "../common/coreservices" ;
65- import { ProviderLike } from "../state/interface" ;
6666import { Resolvable } from "../resolve/resolvable" ;
6767import { ngModuleResolvablesBuilder } from "./statebuilders/lazyLoadNgModuleResolvable" ;
6868import { flattenR } from "../common/common" ;
6969import { UIROUTER_STATES_TOKEN } from "./uiRouterNgModule" ;
7070import { UIRouterRx } from "./rx" ;
71+ import { LocationStrategy , HashLocationStrategy , PathLocationStrategy } from "@angular/common" ;
7172
7273export const NG1_UIROUTER_TOKEN = new OpaqueToken ( "$uiRouter" ) ;
7374
7475/**
75- * This is a provider factory for a UIRouter instance which is configured for Angular 2
76+ * This is a factory function for a UIRouter instance
77+ *
78+ * Creates a UIRouter instance and configures it for Angular 2, then invokes router bootstrap.
79+ * This function is used as an Angular 2 `useFactory` Provider.
7680 */
7781let uiRouterFactory = ( injector : Injector ) => {
7882 // ----------------- ng1-to-ng2 short circuit ------
@@ -140,10 +144,12 @@ let uiRouterFactory = (injector: Injector) => {
140144 return router ;
141145} ;
142146
143- export const _UIROUTER_PROVIDERS : ProviderLike [ ] = [
144- { provide : UIRouterLocation , useClass : UIRouterLocation } ,
147+ export const _UIROUTER_INSTANCE_PROVIDERS : Provider [ ] = [
145148 { provide : UIRouter , useFactory : uiRouterFactory , deps : [ Injector ] } ,
149+ { provide : UIRouterLocation , useClass : UIRouterLocation } ,
150+ ] ;
146151
152+ export const _UIROUTER_PROVIDERS : Provider [ ] = [
147153 { provide : StateService , useFactory : ( r : UIRouter ) => r . stateService , deps : [ UIRouter ] } ,
148154 { provide : TransitionService , useFactory : ( r : UIRouter ) => r . transitionService , deps : [ UIRouter ] } ,
149155 { provide : UrlMatcherFactory , useFactory : ( r : UIRouter ) => r . urlMatcherFactory , deps : [ UIRouter ] } ,
@@ -153,10 +159,41 @@ export const _UIROUTER_PROVIDERS: ProviderLike[] = [
153159 { provide : Globals , useFactory : ( r : UIRouter ) => r . globals , deps : [ UIRouter ] } ,
154160
155161 { provide : UIView . PARENT_INJECT , useFactory : ( r : StateRegistry ) => { return { fqn : null , context : r . root ( ) } as ParentUIViewInject } , deps : [ StateRegistry ] }
156- ]
162+ ] ;
163+
164+ /**
165+ * Provides an Instance of UI-Router for NG2.
166+ *
167+ * Use this on the root NgModule to configure and create an instance of the Angular 2 UIRouter.
168+ *
169+ * @example
170+ * ```js
171+ *
172+ * @UIRouterModule ({
173+ * states: [ homeState, aboutState ],
174+ * providers: [ provideUIRouter({ configClass: MyUIRouterConfig, useHash: true }) ],
175+ * bootstrap: [ UIView ]
176+ * }) class RootNgModule {}
177+ *
178+ * platformBrowserDynamic().bootstrapModule(RootNgModule);
179+ * ```
180+ *
181+ * Note: UIRouter should only be provided *once*, on the root module, when bootstrapping the application.
182+ */
183+ export function provideUIRouter ( rootConfig : { configClass ?: typeof UIRouterConfig , useHash ?: boolean } = { } ) {
184+ // Provide the UIRouter instance providers
185+ return _UIROUTER_INSTANCE_PROVIDERS . concat (
186+ // Provide the user-supplied UIRouterConfig class, or use base UIRouterConfig (as a no-op config)
187+ { provide : UIRouterConfig , useClass : ( rootConfig . configClass as any || UIRouterConfig ) } ,
188+ // Provide the PathLocationStrategy by default unless `useHash` is `true`
189+ { provide : LocationStrategy , useClass : ( rootConfig . useHash ? HashLocationStrategy : PathLocationStrategy ) }
190+ ) ;
191+ }
192+
157193/**
158194 * The UI-Router providers, for use in your application bootstrap
159195 *
160- * @deprecated use [[UIRouterModule]]
196+ * @deprecated use [[UIRouterModule]] and [[provideUIRouter]]
161197 */
162- export const UIROUTER_PROVIDERS = _UIROUTER_PROVIDERS ;
198+ export const UIROUTER_PROVIDERS : Provider [ ] = _UIROUTER_INSTANCE_PROVIDERS . concat ( _UIROUTER_PROVIDERS ) ;
199+
0 commit comments