传递来自subcribe angular2的结果

时间:2017-01-31 12:09:09

标签: angular rxjs

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

我的订阅

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

1 个答案:

答案 0 :(得分:0)

像这样

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