简体   繁体   English

在 Angular 中的 2 个组件之间传递数据

[英]Pass data between 2 components in Angular

I'm trying to pass data from my ag-Grid to a form in a new component by clicking on the row.我正在尝试通过单击该行将数据从我的 ag-Grid 传递到新组件中的表单。 I could get the data from the row by clicking on the cell via onCellClicked .我可以通过onCellClicked单击单元格从行中获取数据。 But i don't know how to pass it to my other component now.但我现在不知道如何将它传递给我的其他组件。 here is my 2 interfaces:这是我的2个界面: 银网格

形式

and here is my code: ag-Grid.ts:这是我的代码: ag-Grid.ts:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Grid, GridApi } from 'ag-grid-community';
import { AgGridAngular } from 'ag-grid-angular';
import { DealsService } from '../services/deals.service';


import * as moment from 'moment';
import { RouterLinkRendererComponent } from '../router-link-renderer/router-link-renderer.component';
@Component({
  selector: 'app-deals',
  templateUrl: './deals.component.html',
  styleUrls: ['./deals.component.scss']
})
export class DealsComponent implements OnInit {
  showNav = true;

  private gridApi;
  gridOptions = {
    rowHeight :90,
    headerHeight:60,

    defaultColDef: {
      sortable: true
  },
  }
  columnDefs = [


       {
      headerName: 'Deal',
      field:'DEALID',
      cellRendererFramework: RouterLinkRendererComponent,
      cellRendererParams: {
        inRouterLink: '/Repo'
      },
      width:300,
      resizable:true,
      onCellClicked: this.makeCellClicked.bind(this),
      filter: 'agNumberColumnFilter',


       },
       {
        headerName:'Block',
        field:'BLOCKID',
        width:200,
        resizable:true,
        filter: 'agNumberColumnFilter',
        columnGroupShow:'open',
      },
      ],
    },
   
];







rowData : any;

constructor(private service:DealsService) {}

onGridReady(params) {
  this.gridApi = params.api; 
}

getDropDownlist(){
  this.service.getDealsList().subscribe(data => this.rowData = data);

  }



  makeCellClicked(event) {
    console.log(event.data);

  }




ngOnInit() {
this.service.getDealsList().subscribe(data => {
  this.rowData = data;
}); 

}
}

I'm blocked and i do really appreciate your help guys.我被阻止了,我真的很感谢你们的帮助。 Thank you!谢谢!

Why not use the service class to fetch the data based on the id or index of data, so example you pass id as a param to new child component and run a ngOninit to fetch that data based on identifier为什么不使用服务 class 来根据数据的 id 或索引获取数据,例如,您将 id 作为参数传递给新的子组件并运行 ngOninit 以根据标识符获取该数据

ngOnInit() {
  this.sub=this._Activatedroute.paramMap.subscribe(params => { 
      const id = params.get('id'); 
      let dealer=this.dealsService.getDealer(id);    
  });

I am creating getDealer() based on assumption you have a get single dealer return function in your service if not then just run dealer.find(dealer => dealer.ID==id);我正在创建 getDealer() 假设您有一个在您的服务中获得单个经销商返回 function 如果不是那么只需运行dealer.find(dealer => dealer.ID==id);

And then on your method in parent route =>然后在父路由中使用您的方法=>

makeCellClicked(event) {
this.router.navigate(['/dealer-details', event.id]);
}

Not sure if this answers your question on how to fetch and display that data row.不确定这是否回答了您关于如何获取和显示该数据行的问题。

The shared service was the right solution.共享服务是正确的解决方案。 How to pass data between two components in Angular 2 如何在 Angular 2 中的两个组件之间传递数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM