I have a network.component which opens my dialog send-tx-dialog.component. The user fills the send-tx-dialog with information. I want to send that information to another component: transaction-pool.component. And after that I want to display the data in a data table dynamically. I tried to use push() but it didn't work.
What is a possible way to do that?
Following some code which I think could be important.
Part of the TS of the dialog:
constructor(
private fb: FormBuilder,
private dialogRef: MatDialogRef<SendTXDialogComponent>,
@Inject(MAT_DIALOG_DATA) data) {
this.sender = data.sender;
this.form = this.fb.group({
sender: [this.sender, Validators.required],
recipient: [this.recipient, Validators.required],
amount: [this.amount, Validators.required],
fee: [this.fee, Validators.required],
releasedAt: [moment(), Validators.required]
});
}
Dialog gets opened in network.component.ts:
openDialog(nodeID) {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.hasBackdrop = false;
dialogConfig.data = {
sender: nodeID,
};
const dialogRef = this.dialog.open(SendTXDialogComponent, dialogConfig);
dialogRef.afterClosed().subscribe(
data => console.log('Send Transaction Output:', data)
);
}
transaction-pool.component.ts:
export class TransactionPoolComponent implements OnInit {
displayedColumns = ['sender', 'recipient', 'amount', 'fee'];
dataSource = ELEMENT_DATA;
transaction: Transaction;
constructor(private _AS: AuthenticationService) {
}
ngOnInit() {
this._AS.transaction.subscribe( transaction => {
this.transaction = transaction;
this.dataSource.push(this.transaction); // This doesn't display the data in the table
}
);
}
}
export interface Transaction {
sender: number;
recipient: string;
amount: number;
fee: string;
}
const ELEMENT_DATA: Transaction[] = [
];
I don't know this is the right way to solve problem but i try to help if it's work for you
network.component.html
network.component.ts
dialog TS:
dialog.html
AuthService:
transaction-pool.component.ts
You should use shared service. In ngAfterViewInit subscribe dialog component to property you want to listen in shared service. In parent component call shared service method that will set property in shared service. Property should be of a type Subject (rxjs)
Take a look at this post: and you will understand everything. Section with shared service. https://netbasal.com/event-emitters-in-angular-13e84ee8d28c Just make sure that you subscribe in ngAfterViewInit in dialog component.
In your shared service you can do:
And after your subribe of the closed dialog do:
And in your other component:
It should look something like this.