How to get id parameter or route parts from Angular route path or url

ngOnInit() {

    //let last_route_part: string = this.route.snapshot.url[this.route.snapshot.url.length - 1].toString();
    //console.log('url=' +    url + ', last_route_part=' + last_route_part); 

    this.route.url.subscribe((url_parts: UrlSegment[]) => {
      //console.log('url_parts=' + url_parts + " last_route_part=" + url_parts[url_parts.length - 1].toString());

      this.last_route_part = url_parts[url_parts.length - 1].toString();

      //console.log('last_route_part1=' + this.last_route_part);
    });
    this.route.params.subscribe((params: Params) => {
      this.edit_record_id = Number(params['id']);
    });
    //console.log('last_route_part2=' + this.last_route_part);

    if (this.last_route_part == 'create') {
      this.create();
    } else if (this.last_route_part == 'edit') {
      //this.edit_record_id = +this.route.snapshot.paramMap.get('id');
      this.edit(this.edit_record_id);
    } else if (this.last_route_part == 'delete') {
      //this.edit_record_id = +this.route.snapshot.paramMap.get('id');
      this.delete(this.edit_record_id);
    }


  }

Leave a Reply

Your email address will not be published. Required fields are marked *