Divya
Divya

Reputation: 11

Cannot read queryParams in Angular

I am trying to fetch queryParams from url with window.location but query params are not being grabbed.

Example my url is www.abc.com?accountId=123# I am looking for a way to grab accountId=123 as a queryParam or atleast the entire url.

When I used window.location I am getting www.abc.com/#/ instead of www.abc.com?accountId=123#

Why am I not able to read the query params?

Why am I not able to read the query params?

Upvotes: 1

Views: 66

Answers (1)

Nathan T.
Nathan T.

Reputation: 298

according to the angular documentation you can fetch the query parameters with the activatedRoute in Angular.

In the below example we grab a query parameter "name".

constructor(
  private route: ActivatedRoute,
) {}

ngOnInit() {
  this.route.queryParams.subscribe(params => {
    this.name = params['name'];
  });
}

Upvotes: 1

Related Questions