Angular2:RxJS Subject not responding to events

房东的猫 提交于 2019-12-02 08:41:05

问题


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:


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



来源:https://stackoverflow.com/questions/45855728/angular2rxjs-subject-not-responding-to-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!