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

Commit fc9976d

Browse files
committed
fix($location): path to work with full url
Changed the way $location.path is handled. Now path function will check if the path contain the base url (protocol://host) is this is there in the path. Mostly user has passed the full url instead of path. We process it differently, by calling the parse method of the respective location object. Since parse method takes care if the url is HTML5 mode , HTML5 hashbang mode or non HTML5 urls and parses them respectively. Currently i have made directly used the parse function which updates the whole location object. We can also ass a parsePath method to each of LocationHashbangInHtml5Url,LocationHashbangUrl, LocationHtml5Url classes which will copy implementation from parse method and get the path out of newUrl but won't update the location object. If this implementation have some issue i can work on making this change. Closes #8617
1 parent 7c60264 commit fc9976d

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/ng/location.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,19 @@ LocationHashbangInHtml5Url.prototype =
415415
* @param {(string|number)=} path New path
416416
* @return {string} path
417417
*/
418-
path: locationGetterSetter('$$path', function(path) {
419-
path = path ? path.toString() : '';
420-
return path.charAt(0) == '/' ? path : '/' + path;
421-
}),
418+
path: function(newPath) {
419+
if (isUndefined(newPath))
420+
return this['$$path'];
421+
var index = newPath.indexOf(appBase);
422+
if ( index >= 0 ) {
423+
this.$$parse(newPath);
424+
return this;
425+
}
426+
newPath = newPath ? newPath.toString() : '';
427+
this['$$path'] = newPath.charAt(0) == '/' ? newPath : '/' + newPath;
428+
this.$$compose();
429+
return this;
430+
},
422431

423432
/**
424433
* @ngdoc method

test/ng/locationSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,16 @@ describe('$location', function() {
652652
expect($browser.url()).toBe('http://new.com/a/b#!/new/path');
653653
}));
654654

655+
it('should update browser when $location path is set using full url', inject(function($rootScope, $browser, $location) {
656+
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
657+
$location.path('http://new.com/a/b#!/new/path');
658+
expect($browserUrl).not.toHaveBeenCalled();
659+
$rootScope.$apply();
660+
661+
expect($browserUrl).toHaveBeenCalledOnce();
662+
expect($browser.url()).toBe('http://new.com/a/b#!/new/path');
663+
expect($location.path()).toBe('/new/path');
664+
}));
655665

656666
it('should update browser only once per $apply cycle', inject(function($rootScope, $browser, $location) {
657667
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();

0 commit comments

Comments
 (0)