Skip to content

Instantly share code, notes, and snippets.

@killerchip
Created January 3, 2018 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killerchip/885d57a6ae8a2da1bd402534d6d730d5 to your computer and use it in GitHub Desktop.
Save killerchip/885d57a6ae8a2da1bd402534d6d730d5 to your computer and use it in GitHub Desktop.
Angular cheatsheet - how to grab URL parameters in routing

Grabbing URL parameters

When 'routing' with Angular you can also have the traditional URL parameters like ?category=toys?maxprice=23.

ActivatedRoute can return those as observables.

...
import { ActivatedRoute } from '@angular/router';

...

export class SearchComponent implements OnInit {

  public searchTermCategory: string = null;
  public searchTermMaxPrice: number = null;

  constructor(
    private activatedRoute: ActivatedRoute
  ) { }

  ngOnInit() {
    this.activatedRoute.queryParams.subscribe(
      data => {
        this.searchTermCategory = data['category'] || null;
        this.searchTermMaxPrice = data['maxprice'] || null;
      }
    )
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment