I'm trying to communicate between my 2 components. In first component I have list with items and I want to send one of this item to my second component to edit them. In first component I set data in my service and in second component I want read this data from service, data is coming but when I set this data to my array after leave ngOnInit method my array is clear.
It is my onClick for edit button in first component
editPurchasesInvoice(i : number) {
this.purchasesService.editPurchasesInvoice(this.purchasesInvoices[i].gpInvoiceitemsByGpInvoiceRecid);
this.router.navigate(['new'], {relativeTo: this.route});
}
Service methods
subject = new Subject<any>();
editPurchasesInvoice(invoiceItems: GpInvoiceitem[]) {
this.subject.next({invoice: invoiceItems});
}
getEditedInvoice(): Observable<any> {
return this.subject.asObservable();
}
it is my scond component
ngOnInit() {
this.subscription = this.purchaseService.getEditedInvoice().subscribe( data => {
this.invoiceItems.push(data.invoice);
});
}
After leave ngOnInit method data is lost.
BehaviorSubjectinstead of aSubjectinside your service