Skip to content

Commit a954e0b

Browse files
committed
URL transitions on param-only state change, fixes angular-ui#289.
1 parent bf38b0d commit a954e0b

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

src/state.js

+25-17
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;
@@ -328,25 +330,31 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
328330
});
329331
}
330332

331-
function normalize(keys, values) {
332-
var normalized = {};
333+
return $state;
334+
}
333335

334-
forEach(keys, function (name) {
335-
var value = values[name];
336-
normalized[name] = (value != null) ? String(value) : null;
337-
});
338-
return normalized;
339-
}
336+
function normalize(keys, values) {
337+
var normalized = {};
340338

341-
function equalForKeys(a, b, keys) {
342-
for (var i=0; i<keys.length; i++) {
343-
var k = keys[i];
344-
if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
345-
}
346-
return true;
339+
forEach(keys, function (name) {
340+
var value = values[name];
341+
normalized[name] = (value != null) ? String(value) : null;
342+
});
343+
return normalized;
344+
}
345+
346+
function equalForKeys(a, b, keys) {
347+
// If keys not provided, assume keys from object 'a'
348+
if (!keys) {
349+
keys = [];
350+
for (var n in a) keys.push(n); // Used instead of Object.keys() for IE8 compatibility
347351
}
348352

349-
return $state;
353+
for (var i=0; i<keys.length; i++) {
354+
var k = keys[i];
355+
if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
356+
}
357+
return true;
350358
}
351359
}
352360

test/stateSpec.js

+15
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)