【问题标题】:pass result from subcribe angular2从订阅 angular2 传递结果
【发布时间】:2017-01-31 12:09:08
【问题描述】:

我创建了一个返回对象数组的服务。如何将服务的结果传递给另一个函数进行进一步处理。

我的订阅

showFeed(){
  this.service.getCompanies().subscribe((companies) => {
     //console.log(companies);
     console.log(companies.rss.channel.item);
     this.items = companies.rss.channel.item;
  });
}

//How do I pass the result of showFeed() to addItem()
additem(){
    this.hostnamesMap={} // hostname as key, and an array of items as value
    this.items.map((item) => {
        var wordCount = item.link.split("/");
        var result = wordCount[0] + "//" + wordCount[2];
        if(!this.hostnamesMap[result]) { // create an entry of not existing
            this.hostnamesMap[result] = [item];
        } else { // add item to already existing entry
            this.hostnamesMap[result].push(item);
        }
    }); 
}

【问题讨论】:

  • 结果在里面,你可以用右箭头访问它。
  • “右箭头”是什么意思
  • this._todos._value 不起作用
  • 上下文没有定义主题。
  • 那么,怎么办?我被卡住了!!!

标签: angular rxjs


【解决方案1】:

喜欢这个

addItem(){
  var todosss = this._todos.subscribe(val => console.log(val)); 
}

注意todossssubscription,您可以致电todosss.dispose()取消订阅

或者不使用 Rx。

showFeed() {
  this.service.getCompanies().subscribe((companies) => {
  // ...
  this.items = companies.rss.channel.item;
  this.additem(); // call additem() here
});

additem(){
  this.hostnamesMap={} // hostname as key, and an array of items as value
  this.items.map((item) => {
   var wordCount = item.link.split("/");
   var result = wordCount[0] + "//" + wordCount[2];
   if(!this.hostnamesMap[result]){ // create an entry of not existing
     this.hostnamesMap[result] = [item];
   }else{ // add item to already existing entry
     this.hostnamesMap[result].push(item);
   }
  }); 
}

【讨论】:

  • 为什么要订阅?
  • @netx 因为这是异步流,你需要订阅才能在你推送下一个时接收一些东西
  • 那么我如何在不使用“next”的情况下传递结果
  • 不要使用rxjs,只需创建带有参数的方法,然后在订阅服务时调用
  • 你能指导我如何做到这一点.. 真的很感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 2017-10-28
  • 2018-11-07
  • 2016-11-17
  • 2017-07-14
  • 1970-01-01
相关资源
最近更新 更多