Skip to content

Instantly share code, notes, and snippets.

@maneja81
Created November 20, 2014 21:15
Show Gist options
  • Save maneja81/4829e0f0bdf994a3a1fc to your computer and use it in GitHub Desktop.
Save maneja81/4829e0f0bdf994a3a1fc to your computer and use it in GitHub Desktop.
Function to format angular objects and arrays to URI Component
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