Skip to content

Instantly share code, notes, and snippets.

@bl4de
Last active September 24, 2018 09:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bl4de/7151557 to your computer and use it in GitHub Desktop.
Save bl4de/7151557 to your computer and use it in GitHub Desktop.
AngularJS service for getting query string vars from url
app.service("UrlService", ["$location", function($location) {
// refernce to service for callbacks
var __service = this,
parts = {
"queryvars": {}
},
absUrl = $location.absUrl(),
// extract and parse url
elements = absUrl.split("?");
// query string
// parse quesry string
parts["queryString"] = elements[1];
if ( elements[1] ) {
parts["hashString"] = (parts["queryString"].split("#"))[1];
parts["requestParams"] = ((parts["queryString"].split("#"))[0]).split("&");
parts["requestParams"].forEach(function(queryStringVariable) {
var __variable = queryStringVariable.split("=");
parts.queryvars[__variable[0]] = __variable[1];
});
}
// url
parts["url"] = elements[0];
// public interface
// returns variable from query string
this.getQueryStringVar = function(variable) {
if (parts.queryvars[variable] !== "undefined") {
return parts.queryvars[variable];
}
return false;
};
}]);
@bmomani1
Copy link

I think we should use

if (parts.queryvars.hasOwnProperty(variable)) {
instead of line 30
https://gist.github.com/bl4de/7151557#file-angularurlservice-js-L30

@bmomani1
Copy link

one more thing line 20
I think it sould be

 parts.queryvars[__variable[0]] = __variable[1]?__variable[1]:"";

instead of the current one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment