Created
November 20, 2014 21:15
-
-
Save maneja81/4829e0f0bdf994a3a1fc to your computer and use it in GitHub Desktop.
Function to format angular objects and arrays to URI Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function serialize(obj) { | |
var str = []; | |
for (var p in obj) { | |
if (angular.isArray(obj[p])) { | |
for (var i = 0; i < obj[p].length; i++) { | |
str.push(encodeURIComponent(p) + "[]=" + obj[p][i]); | |
}; | |
} | |
if (obj.hasOwnProperty(p) && !angular.isArray(obj[p])) { | |
// str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); | |
angular.isArray | |
if (angular.isObject(obj[p])) { | |
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p].key)); | |
} else { | |
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); | |
} | |
} | |
} | |
return str.join("&"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment