首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何合并循环订阅数据angular 2 TS

如何合并循环订阅数据angular 2 TS
EN

Stack Overflow用户
提问于 2017-09-20 19:20:54
回答 1查看 58关注 0票数 0
代码语言:javascript
复制
fieldRender(formSets: any[]) {

    let questions: any[] = [];
    let q: QuestionBase<any>[] = [];
    let index: number = 0;

    formSets.forEach(formSet => {
      formSet.children.forEach(child => {
        this.appointmentService.getField(child.field.uuid).subscribe(res => {
          let dataType;
          let label;
          let parent;
          let uuid;
          let answers = [];

          if (res.concept != null) {
            if (res.concept.datatype)
              dataType = res.concept.datatype.display;
            if (res.answers)
              answers = res.answers;
          }
          uuid = res.uuid;
          label = res.display;
          parent = formSet.parent.field.display;

          let question: Question = new Question();
          question.label = label;
          question.type = dataType;
          question.parent = parent;
          question.uuid = uuid;
          question.answers = answers;

          questions.push(question);

          console.log("Questions", JSON.stringify(questions));
        });
      });
    });

  }

这是我的function.What,我希望这个函数返回可观察到的问题,我可以订阅这些问题,以便在任何地方获取数据。FormSets是包含父数组和子数组的FormSet FormSet数组

EN

回答 1

Stack Overflow用户

发布于 2017-09-20 19:43:33

如果formSet.children是一个数组,您可以执行类似如下的操作

代码语言:javascript
复制
Rx.Observable.from(formSet.children)
.flatMap((child) => this.appointmentService.getField(child.field.uuid))
.subscribe((question) => questions.push(question))

这样,您就可以将来自getField的所有响应放在一个“流”中。

你没有给我们太多的背景信息,所以我猜;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46320682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档