diff --git a/src/transition/transition.ts b/src/transition/transition.ts index b61642e0..5c6d8ed0 100644 --- a/src/transition/transition.ts +++ b/src/transition/transition.ts @@ -276,13 +276,55 @@ export class Transition implements IHookRegistry { * * The `UIInjector` can also provide values from the native root/global injector (ng1/ng2). * + * #### Example: + * ```js + * .onEnter({ entering: 'myState' }, trans => { + * var myResolveValue = trans.injector().get('myResolve'); + * // Inject a global service from the global/native injector (if it exists) + * var MyService = trans.injector().get('MyService'); + * }) + * ``` + * + * In some cases (such as `onBefore`), you may need access to some resolve data but it has not yet been fetched. + * You can use [[UIInjector.getAsync]] to get a promise for the data. + * #### Example: + * ```js + * .onBefore({}, trans => { + * return trans.injector().getAsync('myResolve').then(myResolveValue => + * return myResolveValue !== 'ABORT'; + * }); + * }); + * ``` + * * If a `state` is provided, the injector that is returned will be limited to resolve values that the provided state has access to. + * This can be useful if both a parent state `foo` and a child state `foo.bar` have both defined a resolve such as `data`. + * #### Example: + * ```js + * .onEnter({ to: 'foo.bar' }, trans => { + * // returns result of `foo` state's `data` resolve + * // even though `foo.bar` also has a `data` resolve + * var fooData = trans.injector('foo').get('data'); + * }); + * ``` + * + * If you need resolve data from the exiting states, pass `'from'` as `pathName`. + * The resolve data from the `from` path will be returned. + * #### Example: + * ```js + * .onExit({ exiting: 'foo.bar' }, trans => { + * // Gets the resolve value of `data` from the exiting state. + * var fooData = trans.injector(null, 'foo.bar').get('data'); + * }); + * ``` + * * * @param state Limits the resolves provided to only the resolves the provided state has access to. + * @param pathName Default: `'to'`: Chooses the path for which to create the injector. Use this to access resolves for `exiting` states. + * * @returns a [[UIInjector]] */ - injector(state?: StateOrName): UIInjector { - let path: PathNode[] = this._treeChanges.to; + injector(state?: StateOrName, pathName = "to"): UIInjector { + let path: PathNode[] = this._treeChanges[pathName]; if (state) path = PathFactory.subPath(path, node => node.state === state || node.state.name === state); return new ResolveContext(path).injector(); } diff --git a/test/stateServiceSpec.ts b/test/stateServiceSpec.ts index 5485cdc2..2a562653 100644 --- a/test/stateServiceSpec.ts +++ b/test/stateServiceSpec.ts @@ -592,7 +592,7 @@ describe('stateService', function () { expect(trans.from().name).toBe('design'); expect(trans.to().name).toBe('A'); expect(state.self).toBe($registry.get('design')); - expect(trans.getResolveValue('cc', 'from')).toBe('cc resolve'); + expect(trans.injector(null, 'from').get('cc')).toBe('cc resolve'); } }); @@ -659,7 +659,7 @@ describe('stateService', function () { } }, onEnter: function ($transition$) { - let stateInfo = $transition$.getResolveValue('stateInfo'); + let stateInfo = $transition$.injector().get('stateInfo'); log = stateInfo.join(' => '); } }); diff --git a/test/transitionSpec.ts b/test/transitionSpec.ts index 5464c78d..72551a10 100644 --- a/test/transitionSpec.ts +++ b/test/transitionSpec.ts @@ -404,7 +404,7 @@ describe('transition', function () { resolve: { cc: () => 'cc resolve' }, onExit: (trans, state) => { expect(state.self).toBe(router.stateRegistry.get('design')); - expect(trans.getResolveValue('cc', 'from')).toBe('cc resolve'); + expect(trans.injector(null, 'from').get('cc')).toBe('cc resolve'); done(); } });