Skip to content

Commit 68a09ba

Browse files
pkozlowski-opensourcejeffbcross
authored andcommitted
fix($location): allow numeric location setter arguments
Fixes angular#7054
1 parent 239d0b1 commit 68a09ba

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/ng/location.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,11 @@ LocationHashbangInHtml5Url.prototype =
388388
* Note: Path should always begin with forward slash (/), this method will add the forward slash
389389
* if it is missing.
390390
*
391-
* @param {string=} path New path
391+
* @param {(string|number)=} path New path
392392
* @return {string} path
393393
*/
394394
path: locationGetterSetter('$$path', function(path) {
395+
path = path.toString();
395396
return path.charAt(0) == '/' ? path : '/' + path;
396397
}),
397398

@@ -446,7 +447,8 @@ LocationHashbangInHtml5Url.prototype =
446447
case 0:
447448
return this.$$search;
448449
case 1:
449-
if (isString(search)) {
450+
if (isString(search) || isNumber(search)) {
451+
search = search.toString();
450452
this.$$search = parseKeyValue(search);
451453
} else if (isObject(search)) {
452454
// remove object undefined or null properties
@@ -483,10 +485,12 @@ LocationHashbangInHtml5Url.prototype =
483485
*
484486
* Change hash fragment when called with parameter and return `$location`.
485487
*
486-
* @param {string=} hash New hash fragment
488+
* @param {(string|number)=} hash New hash fragment
487489
* @return {string} hash
488490
*/
489-
hash: locationGetterSetter('$$hash', identity),
491+
hash: locationGetterSetter('$$hash', function(hash) {
492+
return hash.toString();
493+
}),
490494

491495
/**
492496
* @ngdoc method

test/ng/locationSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ describe('$location', function() {
8787
expect(url.absUrl()).toBe('http://www.domain.com:9877/new/path?search=a&b=c&d#hash');
8888
});
8989

90+
it('path() should not break on numeric values', function() {
91+
url.path(1);
92+
expect(url.path()).toBe('/1');
93+
expect(url.absUrl()).toBe('http://www.domain.com:9877/1?search=a&b=c&d#hash');
94+
});
9095

9196
it('search() should accept string', function() {
9297
url.search('x=y&c');
@@ -127,6 +132,13 @@ describe('$location', function() {
127132
});
128133

129134

135+
it('search() should accept numeric keys', function() {
136+
url.search({1: 'one', 2: 'two'});
137+
expect(url.search()).toEqual({'1': 'one', '2': 'two'});
138+
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?1=one&2=two#hash');
139+
});
140+
141+
130142
it('search() should handle multiple value', function() {
131143
url.search('a&b');
132144
expect(url.search()).toEqual({a: true, b: true});
@@ -143,6 +155,8 @@ describe('$location', function() {
143155
it('search() should handle single value', function() {
144156
url.search('ignore');
145157
expect(url.search()).toEqual({ignore: true});
158+
url.search(1);
159+
expect(url.search()).toEqual({1: true});
146160
});
147161

148162

@@ -163,6 +177,13 @@ describe('$location', function() {
163177
});
164178

165179

180+
it('hash() should accept numeric parameter', function() {
181+
url.hash(5);
182+
expect(url.hash()).toBe('5');
183+
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#5');
184+
});
185+
186+
166187
it('url() should change the path, search and hash', function() {
167188
url.url('/some/path?a=b&c=d#hhh');
168189
expect(url.url()).toBe('/some/path?a=b&c=d#hhh');

0 commit comments

Comments
 (0)