Get Query String from Route URI - blazed-space/fire-angular GitHub Wiki
Getting a query string from a route URI element is simple (ex: example.com/home?id=7)
- Import ActivatedRoute (in component.ts):
import { ActivatedRoute } from '@angular/router';
- Add to component class:
...
id: string | undefined;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.queryParams
.subscribe(params => {
this.id = params['id'];
}
);
...
- The query string will now be available to the component as the "id" variable:
<ng-container *ngIf="id">
...
<app-item [itemId]="id"></app-item>
<span>
{{ id }}
</span>
...
</ng-container>