单击时将数据表中的数据行传递到Angular 7中的另一个组件

时间:2019-07-04 16:07:08

标签: angular angular-material angular7 angular8 angular7-router

我想将整行传递给其他组件以显示此数据,任何机构都可以做到这一点。

HTML:

    <mat-row *matRowDef="let row; columns: displayedColumns;" (click)="getRecord(row)"></mat-row>

ts:

  //Get row data when the click
  getRecord(row){
      // Do somthing ..... //
  }

我会搜索很多,我找不到路。

2 个答案:

答案 0 :(得分:1)

完成兄弟姐妹之间的数据传递可能有不止一种方法,但是Angular的方法是使用服务:https://angular.io/guide/component-interaction#parent-and-children-communicate-via-a-service

您还可以通过事件传递数据。让行单击触发父组件接收到的事件,然后通过函数传递给子表的同级对象。

答案 1 :(得分:0)

点击行后,我按照以下步骤解决了问题:

  1. 将数据保存在服务文件中的变量中。
  2. 使用导航进行路由。
  3. 从服务文件中的变量获取数据。
   public  getRecord(row : any){
    console.log(row);
     this.userservice.dataRow = row;
     this.router.navigate(['/profile']);
  }

服务文件:

    //Temporarily stores row data from mat-table
    dataRow:any;

profile.ts:

    this.dataRow = this.userservice.dataRow;