import { ActivatedRoute } from '@angular/router';
export class ExampleComponent implements OnInit {
constructor(private router: ActivatedRoute) { }
ngOnInit() {
this.router.queryParams.subscribe(res=>{
console.log(res) //will give query params as an object
})
}
}
constructor(private router: Router) { }
public myMethodChangingQueryParams() {
const queryParams: Params = { myParam: 'myNewValue' };
this.router.navigate(
[],
{
relativeTo: activatedRoute,
queryParams: queryParams,
queryParamsHandling: 'merge', // remove to replace all query params by provided
});
}
navigateToFoo(){
this._router.navigate([], {
queryParams: {
newOrdNum: '123'
},
queryParamsHandling: 'merge',
});
}
// Make sure to import and define the Angular Router and current Route in your constructor
constructor(
private router: Router,
private route: ActivatedRoute
) {}
...
...
...
// Take current queryParameters from the activated route snapshot
const urlParameters = Object.assign({}, this.route.snapshot.queryParams);
// Need to delete a parameter ?
delete urlParameters.parameterName;
// Need to add or updated a parameter ?
urlParameters.parameterName = newValue;
// Update the URL with the Angular Router with your new parameters
this.router.navigate([], { relativeTo: this.route, queryParams: urlParameters });
export class ComponentA {
// We need access to the Angular router object to navigate programatically
constructor(private router: Router){}
navigateWithArray(): void {
// Create our query parameters object
const queryParams: any = {};
// Create our array of values we want to pass as a query parameter
const arrayOfValues = ['a','b','c','d'];
// Add the array of values to the query parameter as a JSON string
queryParams.myArray = JSON.stringify(arrayOfVAlues);
// Create our 'NaviationExtras' object which is expected by the Angular Router
const navigationExtras: NavigationExtras = {
queryParams
};
// Navigate to component B
this.router.navigate(['/componentB'], navigationExtras);
}
}
getFilteredPersonalBookmarks(searchText: string, limit: number, page: number, userId: string, include: string): Observable<Bookmark[]> {
const params = new HttpParams()
.set('q', searchText)
.set('page', page.toString())
.set('limit', limit.toString())
.set('include', include);
return this.httpClient.get<Bookmark[]>(`${this.personalBookmarksApiBaseUrl}/${userId}/bookmarks`,
{params: params})
.pipe(shareReplay(1));
}