I am trynig to send data from one component to another but it gives me undefined always !
This is my services.ts :
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor() { }
private subjectDossierNumber = new Subject<any>();
sendDossierNumber( dossierNumber : string){
this.subjectDossierNumber.next(dossierNumber);
}
getDossierNumber() : Observable<any>{
return this.subjectDossierNumber.asObservable();
}
}
This is the component which send data :
dossierNumber:string;
constructor(private dataService : DataService){}
ngOnInit(){
this.dossierNumber="2145"}
createNewRequest() { this.dataService.sendDossierNumber(this.dossierNumber); }
And This is the component which receive data :
dossierNumber : string;
constructor(private dataService : DataService){}
ngOnInit() {
this.dataService.getDossierNumber().subscribe(data => {this.dossierNumber = data;})
}
It is going well in the sendDossierNumber() method but in the getDossierNumber() method the data is always undefined. Can anyone know what is the problem please ?