11

i want to achieve that the queryParams can be dynmamically passed it. For now i can set the values of the params dynamically, but not the keys

Here is my Code

onItemClick(item: FilterItem, group: FilterGroup, i: number) {

    let navigationExtras: NavigationExtras = {
      queryParams: { name : group.items[i].param }
    };
     this.router.navigate(['/search'], navigationExtras);
  }

I cannot put instead of "name" my group.name which is a string. I tried this:

 queryParams: { group.name: group.items[i].param }

But here i get syntax error. How can i put the "key" of a queryParam through an object like that?

1 Answer 1

13

This should work:

 queryParams: { [group.name]: group.items[i].param }

See also Creating object with dynamic keys

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

9 Comments

Glad to hear :)
And what will happen if I group.name and group.items[i].param come as blank...will it redirect without parameter...I want to make anchor dynamic for different pages which of them would not have parameters..please respond
Sorry, don't know by heart. Just try it.
Cant get this to work in the template e.g. [queryParams]="{[queryField]: queryValue}". Any ideas?
I guess this is not supported in the template. Assign it to a field in the comonents class and bind to that field.
|

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.