Skip to content

Commit fdc1156

Browse files
committed
URL transitions on param-only state change, fixes #289.
1 parent c2bfa83 commit fdc1156

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/state.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
115115

116116
// Register the state in the global state list and with $urlRouter if necessary.
117117
if (!state['abstract'] && url) {
118-
$urlRouterProvider.when(url, ['$match', function ($match) {
119-
if ($state.$current.navigable != state) $state.transitionTo(state, $match, false);
118+
$urlRouterProvider.when(url, ['$match', '$stateParams', function ($match, $stateParams) {
119+
if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) {
120+
$state.transitionTo(state, $match, false);
121+
}
120122
}]);
121123
}
122124
states[name] = state;
@@ -357,25 +359,31 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
357359
});
358360
}
359361

360-
function normalize(keys, values) {
361-
var normalized = {};
362+
return $state;
363+
}
362364

363-
forEach(keys, function (name) {
364-
var value = values[name];
365-
normalized[name] = (value != null) ? String(value) : null;
366-
});
367-
return normalized;
368-
}
365+
function normalize(keys, values) {
366+
var normalized = {};
369367

370-
function equalForKeys(a, b, keys) {
371-
for (var i=0; i<keys.length; i++) {
372-
var k = keys[i];
373-
if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
374-
}
375-
return true;
368+
forEach(keys, function (name) {
369+
var value = values[name];
370+
normalized[name] = (value != null) ? String(value) : null;
371+
});
372+
return normalized;
373+
}
374+
375+
function equalForKeys(a, b, keys) {
376+
// If keys not provided, assume keys from object 'a'
377+
if (!keys) {
378+
keys = [];
379+
for (var n in a) keys.push(n); // Used instead of Object.keys() for IE8 compatibility
376380
}
377381

378-
return $state;
382+
for (var i=0; i<keys.length; i++) {
383+
var k = keys[i];
384+
if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
385+
}
386+
return true;
379387
}
380388
}
381389

test/stateSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,19 @@ describe('state', function () {
329329
expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
330330
}));
331331
});
332+
333+
describe('url handling', function () {
334+
335+
it('should transition to the same state with different parameters', inject(function ($state, $rootScope, $location) {
336+
$location.path("/about/bob");
337+
$rootScope.$broadcast("$locationChangeSuccess");
338+
$rootScope.$apply();
339+
expect($state.params).toEqual({ person: "bob" });
340+
341+
$location.path("/about/larry");
342+
$rootScope.$broadcast("$locationChangeSuccess");
343+
$rootScope.$apply();
344+
expect($state.params).toEqual({ person: "larry" });
345+
}));
346+
});
332347
});

0 commit comments

Comments
 (0)