简体   繁体   中英

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. I could get the data from the row by clicking on the cell via onCellClicked . But i don't know how to pass it to my other component now. here is my 2 interfaces: 银网格

形式

and here is my code: 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

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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