java - 如何在 Angular 7 中使用字符串添加动态查询参数?

标签 java angular

目前我在屏幕上有多个下拉字段。当选择的下拉值传递到查询参数时,所以我想创建添加的动态查询参数。我的代码如下

     // this is one of the dropdown value
    if (this.SearchText) {
        query += 'q:' + SearchText + ',';
       }
// this is the second one of dropdown value 
    if (this.MakeId) {
        makename = this.SpaceToDash(this.MakesList.find(x => x.Id === +this.MakeId).Name);
        navigateUrl += '/' + makename;
        query += 'merk:' + this.MakeId + ',merkname:' + makename + ',';
       }
    this.router.navigate([ navigateUrl ], { queryParams: { query } });

因此,如果“MakeId”不是下拉值,则不应添加到“queryParams”中,那么我该怎么做。上述解决方案不起作用。

应用 Dynamically create query Params in Angular 2 的解决方案但这不符合我的要求。那么你能帮助我吗?

最佳答案

QueryParams 应采用对象而不是字符串,

所以你可以这样做

let query = {};
// this is one of the dropdown value
if (this.SearchText) {
    query['q'] =  SearchText;
   }
// this is the second one of dropdown value 
if (this.MakeId) {
    makename = this.SpaceToDash(this.MakesList.find(x => x.Id === +this.MakeId).Name);
    navigateUrl += '/' + makename;
    query['merk'] =  this.MakeId;
    query['makename'] =  makename;
   }
this.router.navigate([ navigateUrl ], { queryParams:  query  });

关于java - 如何在 Angular 7 中使用字符串添加动态查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58219297/

相关文章:

java - 使用由父类(super class)集合中的子类实现的接口(interface),无需使用instanceof

java - TreeMap - 为什么即使在其中添加超过 2 个元素后它也只返回 1 的大小?

java - 为什么我的代码没有抛出 EntityExistsException

javascript - 在生产模式 Javascript 上不会加载较少的文件

angular - 命名指令时选择器的名称

angular - 用于根据环境变量更新 config.json 的预构建脚本

java - 适用于 Flex、Java、mongoDb 的云?

java - 测试 Hibernate 存储库时,Spring Boot 不会处理 junit 中的提交

javascript - 新组件加载 Angular 后焦点不会改变

angular - 为什么 Angular 异步管道使用 cdr.markForCheck() 而不是 cdr.detectChanges()?