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

0

2 Answers 2

2
var str = $location.url().substr(1);
Sign up to request clarification or add additional context in comments.

Comments

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.