Angular2:RxJS主题没有响应事件

时间:2017-08-24 07:43:16

标签: javascript angular rxjs subject-observer

我维护一个表,该表是从对象数组生成的,其中单击一行会给出一个包含该特定行数据的对象。我需要与所有其他组件共享它,无论它是父类,孩子或兄弟姐妹,因此我使用主题,类似于https://embed.plnkr.co/P8xCEwSKgcOg07pwDrlO/。我面临的问题是,点击后,我认为事件没有被发射,因为我看到没有任何东西被反映在另一个组件中。请告诉我哪里出错并帮助解决。我的代码如下:

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 个答案:

答案 0 :(得分:1)

您需要在PatientComponent中声明PatientService的提供者,而不是PatientList