How to get query parameters from URL in Angular 5/6/7/8? - cSharp Coder

Latest

cSharp Coder

Sharp Future

Tuesday, November 12, 2019

How to get query parameters from URL in Angular 5/6/7/8?

In Angular 5, the query params are accessed by subscribing to this.route.queryParams.
Example: "/app?param1=hallo&param2=123"
param1: string;
param2: string;
constructor(private route: ActivatedRoute) {
    console.log('Called Constructor');
    this.route.queryParams.subscribe(params => {
        this.param1 = params['param1'];
        this.param2 = params['param2'];
    });
}
whereas, the path variables are accessed by "this.route.snapshot.params"
Example: "/param1/:param1/param2/:param2"
param1: string;
param2: string;
constructor(private route: ActivatedRoute) {
    this.param1 = this.route.snapshot.params.param1;
    this.param2 = this.route.snapshot.params.param2;
}

No comments:

Post a Comment