Reputation: 673
I need to get the GET params from url and pass it in the links to view in angular
I got from location.url()
=> "/?x=1&b=2
"
but I need to get = > "?x=1&b=2
" without slash
I tried to do that like the following:
var str = $location.url();
var x = str.replace(/\\/g, '');
but it kept the slash
Upvotes: 1
Views: 3231
Reputation: 1149
Suggestion:
if you use route get the parameters with $routeParams
you can access the params like:
$routeParams.x
Answer to question:
Do it like a famous question suggests:
How can I get query string values in JavaScript?
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
To be used like:
var x = getParameterByName('x');
Upvotes: 0