Skip to content

Commit 1b0f54d

Browse files
NevilleSRoman-Didenko
authored andcommittedMar 31, 2015
fix($location): allow empty string URLs to reset path, search, and hash
Currently, providing '' to $location#url will only reset the hash, but otherwise has no effect. This change brings the behaviour of $location#url more inline with window.location.href, which when assigned to an empty string loads the page's base href. Before: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com/path After: $location.url() // http://www.example.com/path $location.url('') // http://www.example.com Fixes angular#10063 Closes angular#10064
1 parent 996c7a0 commit 1b0f54d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed
 

‎src/ng/location.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ LocationHashbangInHtml5Url.prototype =
343343
return this.$$url;
344344

345345
var match = PATH_MATCH.exec(url);
346-
if (match[1]) this.path(decodeURIComponent(match[1]));
347-
if (match[2] || match[1]) this.search(match[3] || '');
346+
if (match[1] || url === '') this.path(decodeURIComponent(match[1]));
347+
if (match[2] || match[1] || url === '') this.search(match[3] || '');
348348
this.hash(match[5] || '');
349349

350350
return this;

‎test/ng/locationSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ describe('$location', function() {
245245
expect(url.hash()).toBe('');
246246
});
247247

248+
it('url() should change path when empty string specified', function() {
249+
url.url('');
250+
251+
expect(url.path()).toBe('/');
252+
expect(url.search()).toEqual({});
253+
expect(url.hash()).toBe('');
254+
});
255+
248256

249257
it('replace should set $$replace flag and return itself', function() {
250258
expect(url.$$replace).toBe(false);

0 commit comments

Comments
 (0)