Nithya
Nithya

Reputation: 39

How to decode the URL when navigating one route to another roue in angular 5?

I want to navigate a page from one route another route with query parameters.

var name = "engine & machinery";

this.router.navigateByUrl('dashboard?isopen='+true+'&name='+name);

From this, route navigate to dashboard page but query parameters not coming properly.

Url comes like,

http://localhost:4200/dashboard?isopen=true&name=engine%20&%20machinery=

Getting query parameters by the following code,

console.log("testt", this.route.snapshot.queryParamMap.get('name'));

its return only the "engine".

So, anyone help me for decoding the url and how to get the parameter value?

Thanks in advance

Upvotes: 0

Views: 3495

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39462

Use encodeURIComponent to encode the URL with special characters like these.

var name = encodeURIComponent("engine & machinery");

while creating the URL.

And in the receiving end, decode it using the decodeURIComponent

Something like this:

const encodedName = this.route.snapshot.queryParamMap.get('name')
const decodedName = decodeURIComponent(encodedName);
// This would yield `engine & machinery`

Here's a Sample StackBlitz for your ref.

Upvotes: 3

Related Questions