Send *ngFor value as parameter on click

后端 未结 3 1412
长情又很酷
长情又很酷 2021-02-20 07:47

I\'m making a little application in Angular 2, I\'m displaying a table with some data, I wanna get that data when I make a click on my table

Here\'s the code:

HT

相关标签:
3条回答
  • 2021-02-20 08:11

    use [(ngModel)] to make two way binding of your data to your component. like below

    <td [(ngModel)]="users.username" (click)="data(users.username)">{{users.username}}</td>
    

    Also try

    //$event will hold value and other reference.
    data($event: any) {
    
    }
    

    You can get more reference in angular2 side on [] and () notation

    0 讨论(0)
  • 2021-02-20 08:16

    See the EventEmitter documentation, your custom component should basically look like the example there, implementing the Output() directive and an EventEmitter

    0 讨论(0)
  • 2021-02-20 08:33

    You need to create a public array in the component as like.

    public usersdata:any;

    data(data: string) {
    this.usersdata = data;
    console.log(this.usersdata);
    

    } Now you can access in html template

    <tr *ngFor="let users of usersdata; let i = index">
     <td (click)="data(users.username)">{{users.username}}</td>
     <td (click)="data(users.email)">{{users.email}}</td>
     <td (click)="data(users.id)">{{users.id}}</td>
     <td (click)="data(users.roleId)">{{users.roleId}}</td>
    

    data(data: string) {
    this.selectedParam.emit(data);
    console.log(data);
    

    }

    Above don't keep function name and parameter name same since it will may cause issue. Change name of the parameter

    data(datavalue: string) { 
      this.selectedParam.emit(datavalue); 
      console.log(datavalue); 
    

    }

    0 讨论(0)
提交回复
热议问题