1

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

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Scopi
  • 663
  • 1
  • 6
  • 21

2 Answers2

2
var str = $location.url().substr(1);
Alexey Avdeyev
  • 599
  • 9
  • 20
0

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');
Community
  • 1
  • 1
Anders Vestergaard
  • 1,139
  • 1
  • 14
  • 21