1

I'm aware that I can parse the query string parameters in the browser url using $location.search(), however does angular expose any service to do this manually?

I mean lets say I have the string "www.example.com/my/route?p1=boom&p2=bam"

Does angular give me a way to parse the query string out of it, just like it does internally?

3 Answers 3

1

Based on this answer. You can use Url class in js and did something like this:

var url = new URL("http://www.google.com?query=aaaaa");
console.log(url.search) 

You will get output ?query=aaaaa

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand your question properly... This would give you your query string. Hope it helps.

app.controller('queryController', function($scope, $routeParams){
    $scope.result = $routeParams.p1 + $routeParams.p2;
})

2 Comments

No, I want to parse it from a string not from the browser url bar.
Ah.. ok, aside from just writing the algo, I'm not sure.
0

You can do this using the UrlSerializer

Example :

... myClass {

constructor(private urlSerializer) { }

... myFunc(urlInString: string): void {
  const myParsedUrl = this.urlSerializer.parse(urlInString);
  // Use myParsedUrl, for example :
  // myParsedUrl.queryParams...
}

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.