1

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
    })};
Gayathri
  • 1,776
  • 5
  • 23
  • 50
  • Why are you subscribing to currentdata and not patientdata? – Carsten Aug 24 '17 at 07:47
  • @Gayathri can you try using BehaviourSubject instead of Subject – Rahul Singh Aug 24 '17 at 07:50
  • 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({"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 Aug 24 '17 at 07:55
  • @Carsten I just followed the way it was done previously. Ain't sure of reason . – Gayathri Aug 24 '17 at 07:57
  • 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 Aug 24 '17 at 07:57
  • Well, i actually added providers:[PatientService] in PatientListComponent and patientComponent alone. PatientListComponent is child of patientComponent. patientComponent is child of Appcomponent. Nowhere else. – Gayathri Aug 24 '17 at 08:02
  • Then you need to declare it in `PatientComponent`, not in `PatientList` – Anton Lee Aug 24 '17 at 08:03
  • @AntonLee Genius. You found out exactly where i went wrong. IT's working all well now . Thanks a lot!! – Gayathri Aug 24 '17 at 08:08
  • @AntonLee add an answer – Jota.Toledo Aug 24 '17 at 10:36
  • @Jota.Toledo Thanks for suggesting – Gayathri Aug 24 '17 at 12:26

1 Answers1

1

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

Anton Lee
  • 684
  • 4
  • 10
  • i face the same issue in another hierarchy. /patient is parent. /patient/list is independent and that's sorted out. Then comes /patient/care where carecomponent is again a parent from which comes /patient/care/x, /patient/care/y which needs those data as well . Where and where not providers be given ? – Gayathri Aug 24 '17 at 12:50
  • If you need service in all of this components, and there is no inheritance between them, then declare in AppComponent. Or other single parent component. – Anton Lee Aug 24 '17 at 12:53
  • data doesn't get reflected anywhere else except /patient and /patient/list – Gayathri Aug 24 '17 at 13:24