fix($location): use clone of passed search() object

Fixes bug when $location.search() is not returning search part of current url.

Previously, the location's internal search object could be set by passing an object to the search()
method. Subsequent changes to the passed search object would be exposed when requesting the search
object, but those changes would not appear in the composed url.

Now, the object is cloned, so the result of location.search() should match the contents of
location.absUrl(), provided the object returned from location.search() is not changed.

Closes #9445
This commit is contained in:
bullgare
2014-10-06 18:23:25 +04:00
committed by Caitlin Potter
parent e15d2fd472
commit c7a9009e14
2 changed files with 11 additions and 0 deletions

View File

@@ -473,6 +473,7 @@ var locationPrototype = {
search = search.toString();
this.$$search = parseKeyValue(search);
} else if (isObject(search)) {
search = copy(search, {});
// remove object undefined or null properties
forEach(search, function(value, key) {
if (value == null) delete search[key];

View File

@@ -169,6 +169,16 @@ describe('$location', function() {
});
it('search() should copy object', function() {
var obj = {one: 1, two: true, three: null};
url.search(obj);
expect(obj).toEqual({one: 1, two: true, three: null});
obj.one = 'changed';
expect(url.search()).toEqual({one: 1, two: true});
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?one=1&two#hash');
});
it('search() should change single parameter', function() {
url.search({id: 'old', preserved: true});
url.search('id', 'new');