1

I'm facing an issue while developing an application using Angular 6. While passing an object as a value of a property of queryParams: { prop: value}, built in function converts object to a string, and I get an usual in js [object Object].

What I'm trying to do: I have a query string that i use to send request to my server. I want to use it now, to update query params of current route. Example of my query string stringified with npm-qs lib:

?priceRange[min]=1&priceRange[max] = 2

Let's take a look at my object that I parse with the same lib, back to an object:

priceRange: {min: "1", max: "2"}

What queryParams converts it to:

priceRange=%5Bobject%20Object%5D

To do so, I parse it back to a proper object, and then, I use a method posted somewhere here:

constructor(
  private readonly router: Router,
  private readonly activatedRoute: ActivatedRoute,
) {}

updateQueryParams() {
  this.router.navigate([], {
    relativeTo: this.activatedRoute,
    queryParams: {
      priceRange: {min: "1", max: "2"} (hardcoded this to simplify example)
    }
 });
}

With the code above I'm trying to:

  1. Stay at the same page ([] along with relativeTo: this.activatedRoute, makes me to stay at the same page as far as I understand);

  2. Change url to .com/?priceRange[min]=1&priceRange[max]=2 (or something like that)

I'm asking, is there a way to use objects as a values in queryParams, or are there any suggestions? I've read through angular.io docs, there is nothing about such case, just simple examples of param1=q & param2=w, same actually in several stack posts. Maybe I can somehow pass a query string to my url using Angular means? (Not using those queryParams of router.navigate() method).

Thanks in advance!

1 Answer 1

1

I was also getting this %5Bobject%20Object%5D but then I understood the reason. This is because query params need to be passed as a second argument.

As shown below:

this.router.navigate(["/enter-url"], { 
  queryParams: { 
    priceRange: {
      min: "1",
      max: "2"
    }
  } 
});
Sign up to request clarification or add additional context in comments.

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.