0

I have a table component listing users with a link to redirect to another component in order to edit the record. From table component I call the details component where I want to call a service to fetch me user record. Although I pass the id parameter properly, I am not sure what is the proper way to read it from the details component.

table component:

      <tbody>
        <tr *ngFor="let user of users">
          <td><a routerLink="/users/details/{{user.id}}">{{user.name}}</a></td>
<td>{{user.Name}}</td>
        </tr>
      </tbody>

register router in AppModule:

{ path: 'users/details/:id', component: DisplayUserComponent }

and here is how I plan to call the service from the details component:

export class DisplayUserComponent {

  constructor(private userService: UserService) { }

  ngOnInit() {

    this.userService.getUserById(???????USER_ID????????)
    .subscribe(res => console.log(res));
  }

}

Again, my question is how to get user id from the component which is passed through routing. Any help is welcome!

  • 1
    Did you step through the Angular tutorial? They show exactly how to do this in the Routing section https://angular.io/tutorial/toh-pt5 – Daniel W Strimpel May 21 '18 at 14:47
  • @DanielWStrimpel put in a proper answer to vote it. Thank you so much. –  May 21 '18 at 14:51

1 Answers1

0

There are a lot of ways to read from url parameters. Take a look here

Although, I will go with @DanielWStrimpel and I think you only need this.

Georgios
  • 1,454
  • 6
  • 17
  • 34
  • 1
    Answers that only redirect to external links are usually considered incomplete. The links can become invalid or irrelevant in the future. In addition to providing the links, you should include the relevant information in the answer. – ConnorsFan May 21 '18 at 15:06
  • Thanks @ConnorsFan I see what you mean. – Georgios May 21 '18 at 16:05