Angular 5在父组件上单击按钮时,将单击事件中的数据从父组件传递到子组件

时间:2018-12-10 10:19:28

标签: angular5 angular-event-emitter

我在表中绑定了一些数据,单击任何特定的按钮,我想向其他组件(子组件)显示当前单击的对象中的更多相关数据

例如,我从此链接获取的数据: http://jsonplaceholder.typicode.com/users

HTML代码:

<table>
  <thead>
    <th>
      Id
    </th>
    <th>
      name
    </th>
    <th>
      username
    </th>
    <th>
      email
    </th>
    <th>
      street
    </th>
    <th>
      suite
    </th>
    <th>
      zipcode
    </th>
    <th>
      phone
    </th>
    <th>
      website
    </th>
    </thead>
  <tbody>
    <tr *ngFor="let data of httpdata"> 
      <td>{{data.id}}
      </td> 
      <td>{{data.name}}
      </td> 
      <td>{{data.username}}
      </td> 
      <td>{{data.email}}
      </td> 
      <td>{{data.address.street}}
      </td> 
      <td>{{data.address.city}}
      </td> 
      <td>{{data.address.suite}}
      </td> 
      <td>{{data.address.zipcode}}
      </td> 
      <td>{{data.phone}}
      </td> 
      <td>{{data.website}}
      </td> 
      <td>
        <a routerLink="/conflict-details/conflict" (click)="onSelect(data)">Go to 
        </a> 
      </td> 
    </tr>
  </tbody>
</table>

如果您在单击任何特定数据时看到表格中有一个转到按钮,它应该向我显示有关当前点击次数的完整信息 但是在我的情况下,当我单击转到时,我想将数据绑定到另一个组件中,以明确所有td数据都应显示在新组件(子组件)中。

简单,我想跟踪子组件中选定数据的点击事件。并且表格在父组件中呈现。

if you can see in this screenshot i can bind the data in same component ,

所附的是我的数据表。

the table where i have bind the data

2 个答案:

答案 0 :(得分:2)

尝试

在HTML中 更新

<a routerLink="/conflict-details/conflict" (click)="onSelect(data)">Go to 
        </a> 

<a (click)="onSelect(data)">Go to 
            </a> 

在父组件中

import { Router } from '@angular/router';

export class ParentComponent implements OnInit {
   constructor(private router: Router) {
   }
   onSelect(data) {
      this.router.config.find(r => r.component == ChildComponent).data = data;
      this.router.navigate(["/conflict-details/conflict"]);
   }
}

在子组件中

import { ActivatedRoute } from '@angular/router';

export class ChildComponent implements OnInit {
   SentItem : any;
   constructor(private router: ActivatedRoute) { }
   ngOnInit() {
      this.router.data.subscribe(r=>this.SentItem =r);
  }
}

答案 1 :(得分:1)

您可以使用@Input@Output装饰器来获得所需的输出:

更改:

在父组件中:

HTML代码:

<div>
  <table *ngIf="isVisible === true">
    <tr>
      <th>
        Id
      </th>
      <th>
        name
      </th>
      <th>
        username
      </th>
      <th>
        email
      </th>
    </tr>
    <tr *ngFor="let data of userInformation">
      <td>{{data.id}}
      </td>
      <td>{{data.name}}
      </td>
      <td>{{data.username}}
      </td>
      <td>{{data.email}}
      </td>
      <td>
        <a (click)="onSelect(data)">Go to
        </a>
      </td>
    </tr>
  </table>

  <div *ngIf="isVisible === false">
    <app-test-child [userInfo]="clickedUser" (notify)="backToList($event)"></app-test-child>
  </div>
</div>

TS代码:

局部变量:

userInformation: any;
isVisible : boolean = true;
clickedUser: any;

父组件中的两个函数:

onSelect(data)
{
   this.isVisible = false;
   this.clickedUser = data;
}

backToList(flag) {
  this.isVisible = flag;
  console.log(flag)
}

子组件中:

HTML代码:

<table>
  <tr>
    <th>
      Id
    </th>
    <th>
      name
    </th>
    <th>
      username
    </th>
    <th>
      email
    </th>
  </tr>
  <tr>
    <td>{{clickedUser.id}}
    </td>
    <td>{{clickedUser.name}}
    </td>
    <td>{{clickedUser.username}}
    </td>
    <td>{{clickedUser.email}}
    </td>
    <td>
      <a (click)="backToList()">Back
      </a>
    </td>
  </tr>
</table>

TS代码:

import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';


@Input() userInfo: any;
@Output() notify: EventEmitter<any> = new EventEmitter<any>();

clickedUser: any;

constructor() { }

ngOnInit() {
  this.clickedUser = this.userInfo;
}

backToList() {
  var flag = true;
  this.notify.emit(flag);
}