Last active
September 24, 2018 09:13
-
-
Save bl4de/7151557 to your computer and use it in GitHub Desktop.
AngularJS service for getting query string vars from url
This file contains 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
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; | |
}; | |
}]); |
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
I think we should use
if (parts.queryvars.hasOwnProperty(variable)) {
instead of line 30
https://gist.github.com/bl4de/7151557#file-angularurlservice-js-L30