call function for every row of *ngFor angular2

巧了我就是萌 提交于 2019-12-02 09:43:59

问题


I have to display a table and call a function for every row of table and the function called once for a row.

<tr *ngFor="let item of mf.data"  >
          <td >
            <button (click)="remove(item)" class="btn btn-danger">x</button>
          </td>
          <td>{{item.name}}</td>
          <td>{{item.email}}</td>
          <td class="text-right">{{item.age}}</td>
          <td>{{item.city | uppercase}}</td>
        </tr>

Please suggest how will I implement this functionality??


回答1:


You can add a directive

@Directive({ selector: '[invoke]'})
class InvokeDirective {
  @Output() invoke:EventEmitter = new EventEmitter();
  ngAfterContentInit() {
    this.invoke.emit(null);
  }
}

And the use it like

<tr *ngFor="let item of mf.data" (invoke)="myFunction(item)" >


来源:https://stackoverflow.com/questions/42266551/call-function-for-every-row-of-ngfor-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!