Scopi
Scopi

Reputation: 673

remove slash from $location.url() in angular

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

Answers (2)

Alexey Avdeyev
Alexey Avdeyev

Reputation: 609

var str = $location.url().substr(1);

Upvotes: 2

Anders Vestergaard
Anders Vestergaard

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

Related Questions