Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f41ca4a

Browse files
omsmithpetebacondarwin
authored andcommitted
fix(ngRoute): dont duplicate optional params into query
When calling updateParams with properties which were optional, but previously undefined, they would be duplicated into the query params as well as into the path. Closes #10689
1 parent 0bcd087 commit f41ca4a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/ngRoute/route.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,10 @@ function $RouteProvider() {
481481
*/
482482
updateParams: function(newParams) {
483483
if (this.current && this.current.$$route) {
484-
var searchParams = {}, self=this;
485-
486-
angular.forEach(Object.keys(newParams), function(key) {
487-
if (!self.current.pathParams[key]) searchParams[key] = newParams[key];
488-
});
489-
490484
newParams = angular.extend({}, this.current.params, newParams);
491485
$location.path(interpolate(this.current.$$route.originalPath, newParams));
492-
$location.search(angular.extend({}, $location.search(), searchParams));
486+
// interpolate modifies newParams, only query params are left
487+
$location.search(newParams);
493488
}
494489
else {
495490
throw $routeMinErr('norout', 'Tried updating route when with no current route');

test/ngRoute/routeSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,30 @@ describe('$route', function() {
13411341
});
13421342
});
13431343

1344+
it('should not update query params when an optional property was previously not in path', function() {
1345+
var routeChangeSpy = jasmine.createSpy('route change');
1346+
1347+
module(function($routeProvider) {
1348+
$routeProvider.when('/bar/:barId/:fooId/:spamId/:eggId?', {controller: angular.noop});
1349+
});
1350+
1351+
inject(function($route, $routeParams, $location, $rootScope) {
1352+
$rootScope.$on('$routeChangeSuccess', routeChangeSpy);
1353+
1354+
$location.path('/bar/1/2/3');
1355+
$location.search({initial: 'true'});
1356+
$rootScope.$digest();
1357+
routeChangeSpy.reset();
1358+
1359+
$route.updateParams({barId: '5', fooId: '6', eggId: '4'});
1360+
$rootScope.$digest();
1361+
1362+
expect($routeParams).toEqual({barId: '5', fooId: '6', spamId: '3', eggId: '4', initial: 'true'});
1363+
expect(routeChangeSpy).toHaveBeenCalledOnce();
1364+
expect($location.path()).toEqual('/bar/5/6/3/4');
1365+
expect($location.search()).toEqual({initial: 'true'});
1366+
});
1367+
});
13441368

13451369
it('should complain if called without an existing route', inject(function($route) {
13461370
expect($route.updateParams).toThrowMinErr('ngRoute', 'norout');

0 commit comments

Comments
 (0)