Skip to content

Commit 8c1bf30

Browse files
committed
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)
1 parent d31b333 commit 8c1bf30

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/state.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
10481048

10491049
// Filter parameters before we pass them to event handlers etc.
10501050
toParams = filterByKeys(to.params.$$keys(), toParams || {});
1051-
1051+
1052+
// Re-add the saved hash before we start returning things or broadcasting $stateChangeStart
1053+
if (hash) toParams['#'] = hash;
1054+
10521055
// Broadcast start event and cancel the transition if requested
10531056
if (options.notify) {
10541057
/**
@@ -1126,9 +1129,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
11261129
}
11271130
}
11281131

1129-
// Re-add the saved hash before we start returning things
1130-
if (hash) toParams['#'] = hash;
1131-
11321132
// Run it again, to catch any transitions in callbacks
11331133
if ($state.transition !== transition) return TransitionSuperseded;
11341134

test/stateSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,18 @@ describe('state', function () {
570570
expect($location.url()).toBe('/front/world#frag');
571571
expect($location.hash()).toBe('frag');
572572
}));
573+
574+
it('has access to the #fragment in $stateChangeStart hook', inject(function ($state, $q, $location, $rootScope) {
575+
var hash_accessible = false;
576+
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
577+
hash_accessible = toParams['#'] === 'frag';
578+
});
579+
580+
$state.transitionTo('home.item', {id: 'world', '#': 'frag'});
581+
$q.flush();
582+
583+
expect(hash_accessible).toBe(true);
584+
}));
573585
});
574586

575587
describe('.go()', function () {

0 commit comments

Comments
 (0)