简体   繁体   English

等待订阅完成并调用 angular6 中的保存?

[英]wait for subscribe to finish and call save in angular6?

I have to complete validation functions before calling the save function.我必须在调用保存 function 之前完成验证功能。 I am calling API (Async in C#) for validation function and it does not wait until the return result and proceeding in to save which is wrong.我正在调用 API(C# 中的异步)进行验证 function 并且它不会等到返回结果并继续保存这是错误的。

submit() {        
       this.validateBeforeSave();
       this.Save();        
      }
    
     validateBeforeSave()
     {
      // Here calling API action ( used Async and Await in C#)
          forkJoin([Action1URL, Action2URL])             
        .subscribe(([Action1Data, Action2Data]) => {
           this.ValidateData(
              Action1Data,
              Action2Data
            );
           // Don't want to call save here. should be called separately.
        });   
     }
     
     Save()
     {
     // Saving data.
     
     }

Please let me know how to wait for the return result from the subscribe and then call the save function?请让我知道如何等待订阅返回结果然后调用保存 function? And how can I call save function without placing inside subscribe?我怎么能在没有订阅的情况下调用 save function ?

It's not possible to call save() function separately after subscribe, While it hits subscribe it will redirect to next line after execution of all lines next to validateBeforeSave() function and then code inside subscribe will be executed.订阅后无法单独调用 save() function ,当它点击订阅时,它将在执行 validateBeforeSave() function 旁边的所有行后重定向到下一行,然后将执行订阅中的代码。


submit() {        
       this.validateBeforeSave();
      
      }
    
     validateBeforeSave()
     {
      // Here calling API action ( used Async and Await in C#)
          forkJoin([Action1URL, Action2URL])             
        .subscribe(([Action1Data, Action2Data]) => {
           this.ValidateData(
              Action1Data,
              Action2Data
            );
           this.Save();        
        });   
     }
     
     Save()
     {
     // Saving data.
     
     }

Assuming Validate method returns a boolean that lets you know if you should save or not.假设 Validate 方法返回一个 boolean 让您知道是否应该保存。

submit() {
      this.validateBeforeSave()
        .subscribe((isFormValid: boolean) => {
          if (isFormValid) { // repending on result of ValidateData
            this.Save();
          }
        });
    }
    
    validateBeforeSave(): Observable < boolean > { // returns observable
      // Here calling API action ( used Async and Await in C#)
      forkJoin([Action1URL, Action2URL])
      .pipe(map([Action1Data, Action2Data]) => {
        return this.ValidateData(Action1Data, Action2Data); // returning boolean returned from validateData
      });
    }
    
    Save() {
      // Saving data
    }

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

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