0

I trying in *ngFor pass object to another component, but passed only last object in table, but the object that was clicked should be displayed, how to solve this problem?

  <tr data-toggle="control-sidebar" *ngFor="let user of pagedItems">
      <td><control-sidebar [user]="user">
  </control-sidebar>{{ user.name }}</td>

and in another component view showing only last table row.

llotall
  • 555
  • 5
  • 11
  • why are you passing `userDetails` instead of the `user` object ? – Abrar Jun 29 '17 at 14:54
  • See this example. It shows how to pass a clicked object from `*ngFor` loop to another component. https://angular.io/generated/live-examples/toh-pt3/eplnkr.html – Nehal Jun 29 '17 at 14:58

1 Answers1

0

When you are binding the user data to the control-sidebar:

[user]="userDtails", the problem lies here.

Modify the code :

<tr data-toggle="control-sidebar" *ngFor="let user of pagedItems">
      <td (click)="getUser(user)"><control-sidebar [user]="user">
  </control-sidebar>{{ user.name }}</td>

Make sure you have defined the getUser method in your component class.

Notice that I have set: [user]="user". SInce you want to pass the user object into control-sidebar.

Everything should work fine now.

EDIT :

Since you want to display the user that was clicked you can bind a method to the (click) handler and pass the user object. for example:

(click)="showUser(user)"
Abrar
  • 6,874
  • 9
  • 28
  • 41