Dynamically create query Params in Angular 2

馋奶兔 提交于 2020-01-11 08:37:05

问题


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:


This should work:

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

See also Creating object with dynamic keys



来源:https://stackoverflow.com/questions/42889714/dynamically-create-query-params-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!