1
votes

I maintain a table which is generated from array of objects in which on click on a row would give an object which contain data of that one particular row.I needed to share it with all other components no matter it is a parent,child or sibling and and hence i use Subject,similar to https://embed.plnkr.co/P8xCEwSKgcOg07pwDrlO/. The problem i face is, on click, I assume event isnot getting emitted due to which I see nothing being reflected in another component. PLease let me know where i went wrong and help resolving.My code is below:

patientlist.component.ts

 getPatientDetail(value)  { //value gets passed on click of row in table
        this.patientService.getPatientDetail(value);
   }

patient.service.ts

         patientdata=new Subject<Object>();
         currentdata=this.patientdata.asObservable();
          getPatientDetail(data:Object){
          console.log(data);
          this.patientdata.next(data);
          }

patient.component.ts

 constructor(private patientService: PatientService) {

   this.patientService.currentdata.subscribe(data=>{
    console.log("am here"); //even this doesn't get reflected
    this.data=data;
    console.log(this.data); //nothing here as well
    })};
1
Why are you subscribing to currentdata and not patientdata? - Carsten
@Gayathri can you try using BehaviourSubject instead of Subject - Rahul Singh
Both doesn't make a difference Rahul,except for the fact that in behaviorsubject, you can set a default value which get logged when there is no data. LIke the one we discussed yesterday. If line is patientdata=new Subject<Object>({"message":"nothing"); , if there is no data, it'll be {"message":"nothing"}. Yesterday i set it as blank {} so it displayed {}. That's it - Gayathri
@Carsten I just followed the way it was done previously. Ain't sure of reason . - Gayathri
How did you declared provider of PatientService? Maybe you have different instances of it. You need to declare it in root Component of your patient and patientlist - Anton Lee

1 Answers

1
votes

You need to declare provider of PatientService in PatientComponent, not in PatientList