简体   繁体   English

如何在角度7中将预订结果从一种方法返回到另一种方法?

[英]How to return a subscription result from one method to another in angular 7?

I have a method to upload a file 我有一种上传文件的方法

 uploadFiles(event, type) {
            var files = event.srcElement.files;
            for (let i = 0; i < files.length; i++) {
                formData.append("uploads[]", files[i], files[i]['name']);
            }

            formData.append('formFlags', type);
            return await this.uploadService.upload(formData).subscribe(response => {
               //return response
            });

the above upload function is called from another function like 上面的上传函数是从另一个函数调用的

uploadMarketingAgreement(event) {
    this.uploadFiles(event, "profilepic");
   //how to get result of the above method in a varaible
}

how can i return the result of first (asynchronous) function to the second function? 如何将第一个(异步)函数的结果返回给第二个函数?

I have a mocked service which returns a simple function... working demo here... you can use the value as soon as the service returns a call 我有一个模拟的服务,它返回了一个简单的函数... 这里工作的演示 ...您可以在服务返回调用后立即使用该值

constructor(private uService: UploadService) {
    this.uploadMarketingAgreement('');
  }

  uploadFiles(event, type) {
    return new Promise((resolve, reject) => {

      this.uService.upload(this.formData).subscribe(
        response => { console.log("data from service:" + response); resolve(response); }
        , errr => { console.log("error from service:" + errr); reject(errr); }
      );
    });
  }

  async uploadMarketingAgreement(event) {
    const uploadFilesVal = await this.uploadFiles(event, "profilepic");
    console.log('from await:' + uploadFilesVal);
    //how to get result of the above method in a varaible
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM