From a26acb64fe2ed3e05bf21ac1c058d6ac59b89870 Mon Sep 17 00:00:00 2001 From: Sekib Omazic Date: Thu, 6 Mar 2014 12:33:07 +0100 Subject: [PATCH] fix($location): remove query args when passed in object Query args will be removed from $location search object if they are passed in as null or undefined object properties Closes #6565 --- src/ng/location.js | 5 +++++ test/ng/locationSpec.js | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/ng/location.js b/src/ng/location.js index 82a9843ad68d..d0196ae25c80 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -446,6 +446,11 @@ LocationHashbangInHtml5Url.prototype = if (isString(search)) { this.$$search = parseKeyValue(search); } else if (isObject(search)) { + // remove object undefined or null properties + forEach(search, function(value, key) { + if (value == null) delete search[key]; + }); + this.$$search = search; } else { throw $locationMinErr('isrcharg', diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 3bd4c97670b6..8b16de2bf86c 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -117,6 +117,15 @@ describe('$location', function() { }); + it('search() should remove multiple parameters', function() { + url.search({one: 1, two: true}); + expect(url.search()).toEqual({one: 1, two: true}); + url.search({one: null, two: null}); + expect(url.search()).toEqual({}); + expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b#hash'); + }); + + it('search() should handle multiple value', function() { url.search('a&b'); expect(url.search()).toEqual({a: true, b: true});