2

I'm using Angular2 (RC2) and the new Router (Alpaha.8) How can I stop a query param showing in the URL if it is undefined? Eg.

this.router.navigate(["/results", this.month, this.day],
    {
       queryParams: {
          adults: +this.adults
    }
});

Sometimes adults may have a value and other times it may not which is when I don't want to show it.

2 Answers 2

1

I don't think there is another way than just not to add them when they are undefined:

let queryParams:any = {};
if(this.adults) queryParams.adults = this.adults;

this.router.navigate(["/results", this.month, this.day],
    {
       queryParams: queryParams
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I added let queryParams: any = {};
0
let queryParams = { paramToAdd: "newValue", paramToRemove: undefined };

router.navigate(['.'], { queryParams, queryParamsHandling: "merge", relativeTo: this.route.parent })

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.