Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更新angular2中组件之间的事件数据?_Javascript_Angular - Fatal编程技术网

Javascript 如何更新angular2中组件之间的事件数据?

Javascript 如何更新angular2中组件之间的事件数据?,javascript,angular,Javascript,Angular,我正在使用两个平行的angular2组件,并尝试在单击组件1时更新组件2中的数据 代码如下: 共享服务: class updateData { constructor (){ this._req = {}; this.observer; this.updateReq = new Rx.Observable(function (observer) { return this.observer = observer; }).share(

我正在使用两个平行的
angular2
组件,并尝试在单击组件1时更新组件2中的数据

代码如下:

共享服务:

class updateData {
  constructor (){
    this._req = {};
    this.observer;
    this.updateReq = new Rx.Observable(function (observer) {
              return this.observer = observer;
    }).share();
  }
  update(req) {
    this._req = req;
    this.observer.next(req);
  }
};
构成部分1:

app.getLogs = ng.core.Component({
    selector: 'get-logs',
    providers: [updateData, ng.http.HTTP_PROVIDERS, httpService],
    template: "<button class='btn btn-success pull-left mrT2' (click)='getLogfilters()' >Get Logs</button>"
  })

  .Class({
    constructor: [updateData,ng.http.Http, httpService, function (updateData, http, service){
      this.result = {};
      this.http = http;
      this.service = service;
      this.updateData = updateData;
      this.reqBody = {};
    }],

    getLogfilters: function(){
      var fed = (function(){
        return this;
      }).bind(this);

      setTimeout(function(){
        var self = fed();
        self.service.getLogs(req, selectedCompany).subscribe(function(result){
          this.result = JSON.parse(result._body);

          //--------- UPDATE DATA HERE--------------//
          self.updateData.update(req);

        }.bind(this), function(error){
          error = JSON.parse(error._body);
          console.log(error.code, error.message);
        });

      });
    }
  });
每当我单击组件2并尝试运行该组件时,我都会收到一个错误:

“无法读取未定义的属性'next'。”

位于

this.updateReq = new Rx.Observable(function (observer) {
          return this.observer = observer;
}).share();
这不是你所期望的吗

`function (observer)` in
要么换成

(observer) => 

this.updateReq = new Rx.Observable(function (observer) {
          return this.observer = observer;
}.bind(this)).share();

另请参见

我没有进一步查看。这可能需要更多的
函数()…
。通常你想在任何地方都使用
=>
。谢谢你的回复:),我正在努力解决这个问题。
this.updateReq = new Rx.Observable(function (observer) {
          return this.observer = observer;
}.bind(this)).share();