Angular2:RxJS主题没有响应事件 [英] Angular2:RxJS Subject not responding to events

查看:93
本文介绍了Angular2:RxJS主题没有响应事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

patientlist.component.ts

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

patient.service.ts

patient.service.ts

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

patient.component.ts

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
    })};


推荐答案

您需要在<$ c中声明PatientService的提供者$ c> PatientComponent ,不在 PatientList

这篇关于Angular2:RxJS主题没有响应事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆