0

I have a URL which is like this

http://localhost:4200/cr/hearings?UserID=61644&AppID=15&AppGroupID=118&SelectedCaseID=13585783&SelectedRoleID=0

The router module knows to display based on this

{ path: 'criminal/hearings', component: HearingsComponent, pathMatch: 'full'},

but now I have a button click in which I want to maintain the querystring.

button html

<button type="button" (click)="addHearing()" class="btn btn-primary">Add Hearing</button>

typescript function

addHearing(){
    this.router.navigate(['/cr/edithearings']) // how to include the querystring???
}

I want to add the existing querystring to above button click event then in the route module it goes to proper route, along with the querystring

{ path: 'criminal/edithearings', component: HearingEditComponent, pathMatch: 'full'},

http://localhost:4200/criminal/edithearings?HOW_to_add?

2 Answers 2

3

If you are navigating using HTML template, you can use preserveQueryParams="true"

Notice that preserveQueryParams is without a square bracket.

Eg:

<a [routerLink]="['/navigate-to']" preserveQueryParams="true">

In-code example :

addHearing(){
    this.router.navigate(['somewhere'], { queryParamsHandling: "preserve" });
}
Sign up to request clarification or add additional context in comments.

3 Comments

so do this in the html instead? I have a <button> with (click) ... function call and so it is in typescript then
SCORE, TY very much.
@ScottLeary : Welcome :)
2
addHearing(){
    this.router.navigate(['/cr/edithearings'], { queryParams: { key: value } })
}

navigate has a queryParams parameter

3 Comments

so it will add in all the existing url params?
You have to set it. Take the params from the url and add it to the addHearing function.
how do i set it? key: value value is red line under it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.