问题
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!
回答1:
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.
回答2:
The shared service was the right solution.
来源:https://stackoverflow.com/questions/62487263/pass-data-between-2-components-in-angular