Search code examples
javascriptangularmeandynamic-url

Not able to get the parameters from the dynamic url in Angular2


I'm trying to have a route called 'event' with parameter event_id in the URL.

app routing module has this

{ path: 'event/:event_id', component: EventComponent },

The component tries to get event_id from url using Activated route.

import { ActivatedRoute, Params } from '@angular/router';
export class EventComponent implements OnInit {
  event: JSON;
  id: String;
  constructor(private route: ActivatedRoute) {
  }
  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      console.log('that kind of madness can\'t be undone');
      console.log(params['event_id']); // checking result
    })
  }

console.log(params['event_id']) is giving an empty object as a result.


Solution

  • :eventId isn't a query parameter, it would belongs to route parameter, so check params Observable on current activate route.

    this.route.params.subscribe(params => {
      console.log('that kind of madness can\'t be undone');
      console.log(params['event_id']); // checking result
    })