如何在循环中调用多个订阅

时间:2019-06-20 05:44:27

标签: angular7

这是我的代码:

executeService(): void {
  var node = this.node;
  while(node.val != 0){
    this.httpClientService.executeService(node).
    subscribe( data => {
      alert("Run successfully");
      this.output = data; // getting value in this.output
      // Service call to update table.
    });
    node = node.childNode;
    node.prevOutput = this.output; // not getting output
  }
}

我无法在订阅闭包之外获得输出值。

1 个答案:

答案 0 :(得分:0)

请共享您的Node数据结构,但您可以使用递归调用:

executeService():void{
  recursiveServiceCall(this.node);
}

 recursiveServiceCall(node:any){

  this.httpClientService.executeService(node).
   subscribe( data => {
      node.prevOutput=data;
      if(node.childNode && node.val !=0)
         recursiveServiceCall(node.childNode);
      });
  }