How to handle multiple queryParams in Angular2

后端 未结 2 1682
花落未央
花落未央 2021-01-13 11:54

I\'m trying to implement a filtering mechanism in a new Angular2 app, that would allow me to filter a list of entries in an array. The entries may have around 20 properties

2条回答
  •  余生分开走
    2021-01-13 12:19

    You could try it like this:

    Define an array with your query params:

    myQueryParams = [
        { id: 1, param: 'myParam' },
        { id: 2, param: 'myParam' },
        { id: 3, param: 'myParam' },
        { id: 4, param: 'myParam' },
        { id: 5, param: 'myParam' },
    ];
    

    Put this array into one single query param:

    this.router.navigate(['/findlist'], {
        queryParams: {
            filter: JSON.stringify(this.myQueryParams)
        }
    });
    

    Read the query param like this:

    this.route.queryParams.subscribe((p: any) => {
        if (p.filter){
            console.log(JSON.parse(p.filter));
        }
    });
    

    You'll see something like this:

    Now you can parse this object. I think the URL looks a little bit ugly, but it should be functional. Try it out, I hope it helps you ;)

提交回复
热议问题