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

feat(ngRoute): add method for changing url params #8358

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,21 @@ function $RouteProvider(){
reload: function() {
forceReload = true;
$rootScope.$evalAsync(updateRoute);
},

/**
* @ngdoc method
* @name $route#updateParams
*
* @description
* Causes `$route` service to update the current URL, replacing
* current route parameters with those specified in `newParams`.
*
* @param {Object} newParams mapping of URL parameter names to values
*/
updateParams: function(newParams) {
newParams = angular.extend({}, this.current.params, newParams);
$location.path(interpolate(this.current.$$route.originalPath, newParams));
}
};

Expand Down
72 changes: 71 additions & 1 deletion test/ngRoute/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,6 @@ describe('$route', function() {
});
});


describe('reload', function() {

it('should reload even if reloadOnSearch is false', function() {
Expand Down Expand Up @@ -1078,4 +1077,75 @@ describe('$route', function() {
});
});
});

describe('update', function() {
it('should support single-parameter route updating', function() {
var routeChangeSpy = jasmine.createSpy('route change');

module(function($routeProvider) {
$routeProvider.when('/bar/:barId', {controller: angular.noop});
});

inject(function($route, $routeParams, $location, $rootScope) {
$rootScope.$on('$routeChangeSuccess', routeChangeSpy);

$location.path('/bar/1');
$rootScope.$digest();
routeChangeSpy.reset();

$route.updateParams({barId: '2'});
$rootScope.$digest();

expect($routeParams).toEqual({barId: '2'});
expect(routeChangeSpy).toHaveBeenCalledOnce();
expect($location.path()).toEqual('/bar/2');
});
});

it('should support total multi-parameter route updating', function() {
var routeChangeSpy = jasmine.createSpy('route change');

module(function($routeProvider) {
$routeProvider.when('/bar/:barId/:fooId/:spamId/:eggId', {controller: angular.noop});
});

inject(function($route, $routeParams, $location, $rootScope) {
$rootScope.$on('$routeChangeSuccess', routeChangeSpy);

$location.path('/bar/1/2/3/4');
$rootScope.$digest();
routeChangeSpy.reset();

$route.updateParams({barId: '5', fooId: '6', spamId: '7', eggId: '8'});
$rootScope.$digest();

expect($routeParams).toEqual({barId: '5', fooId: '6', spamId: '7', eggId: '8'});
expect(routeChangeSpy).toHaveBeenCalledOnce();
expect($location.path()).toEqual('/bar/5/6/7/8');
});
});

it('should support partial multi-parameter route updating', function() {
var routeChangeSpy = jasmine.createSpy('route change');

module(function($routeProvider) {
$routeProvider.when('/bar/:barId/:fooId/:spamId/:eggId', {controller: angular.noop});
});

inject(function($route, $routeParams, $location, $rootScope) {
$rootScope.$on('$routeChangeSuccess', routeChangeSpy);

$location.path('/bar/1/2/3/4');
$rootScope.$digest();
routeChangeSpy.reset();

$route.updateParams({barId: '5', fooId: '6'});
$rootScope.$digest();

expect($routeParams).toEqual({barId: '5', fooId: '6', spamId: '3', eggId: '4'});
expect(routeChangeSpy).toHaveBeenCalledOnce();
expect($location.path()).toEqual('/bar/5/6/3/4');
});
});
});
});