From 8c1bf30d2a3b78ba40b330f12d854c885d6cc117 Mon Sep 17 00:00:00 2001 From: Alexandre Boukhlif Date: Fri, 15 Jan 2016 16:46:15 +0100 Subject: [PATCH] fix(transitionTo): re-added the saved hash before broadcasting event Re-added the saved hash before broadcasting $stateChangeStart. This way, libraries using this event to do their magic will have the hash accessible through toParams. (e.g. https://github.com/Narzerus/angular-permission) --- src/state.js | 8 ++++---- test/stateSpec.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/state.js b/src/state.js index be36c99bc..ce985b56c 100644 --- a/src/state.js +++ b/src/state.js @@ -1048,7 +1048,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { // Filter parameters before we pass them to event handlers etc. toParams = filterByKeys(to.params.$$keys(), toParams || {}); - + + // Re-add the saved hash before we start returning things or broadcasting $stateChangeStart + if (hash) toParams['#'] = hash; + // Broadcast start event and cancel the transition if requested if (options.notify) { /** @@ -1126,9 +1129,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { } } - // Re-add the saved hash before we start returning things - if (hash) toParams['#'] = hash; - // Run it again, to catch any transitions in callbacks if ($state.transition !== transition) return TransitionSuperseded; diff --git a/test/stateSpec.js b/test/stateSpec.js index cecde231c..c73a7fa0c 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -570,6 +570,18 @@ describe('state', function () { expect($location.url()).toBe('/front/world#frag'); expect($location.hash()).toBe('frag'); })); + + it('has access to the #fragment in $stateChangeStart hook', inject(function ($state, $q, $location, $rootScope) { + var hash_accessible = false; + $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { + hash_accessible = toParams['#'] === 'frag'; + }); + + $state.transitionTo('home.item', {id: 'world', '#': 'frag'}); + $q.flush(); + + expect(hash_accessible).toBe(true); + })); }); describe('.go()', function () {