Angular中的链服务可观察呼叫

时间:2020-04-29 14:23:26

标签: angular rxjs observable

  • 我有一个要上传的文件列表。
  • 只有在所有文件成功上传后,我才想做某事
 onUpload(event) {
    let files: File[] = event;
    files.forEach(file => {
      const formData = new FormData();
      formData.append('file', file);
      formData.append('dsId', this.rdd.tech_id);
      this.rddService.upload(formData).subscribe(response => {
        // Do Something 
      })
    })
  }

有办法吗?

1 个答案:

答案 0 :(得分:1)

尝试forkJoinhttps://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin

import { forkJoin } from 'rxjs';

onUpload(event) {
    let files: File[] = event;
    forkJoin(
      // spread the array of requests
     // in the map we do the appropriate modifications as well
      ...files.map(file => {
          const formData = new FormData();
          formData.append('file', file);
          formData.append('dsId', this.rdd.tech_id);
          return this.rddService.upload(formData)
      })
    ).subscribe(data => {
     console.log('All done', data);
   });
  }