Sambhav jain
Sambhav jain

Reputation: 27

change the variable if specific param is present in url

how can I use this.router.url to check if my my URL contain specific parameter such as id? In one instance i am using following if url has profile to change search icon. i want to same for user/videos/:id but only when the id param is present in url

    if (this.url.includes("profile")) {
      this.searchIcon = true;
    }

Upvotes: 0

Views: 19

Answers (1)

Barremian
Barremian

Reputation: 31125

For a quick fix you could use the paramMap snapshot from ActivatedRoute.

import { ActivatedRoute } from '@angular/router';
...
export class SomeComponent implements OnInit {
  id: any;
  constructor(private _activatedRoute: ActivatedRoute) { }

  ngOnInit() {
    this.id = this._activatedRoute.snapshot.paramMap.get('id');
    if (!!this.id) {
      // `id` defined
    } else {
      // `id` undefined
    }
  }
}

Upvotes: 1

Related Questions