简体   繁体   English

如何在 Angular 2 中继续之前等待可观察方法完成

[英]How to wait for a observable method to finish before proceeding in Angular 2

I am new to Angular and I am working on fixing something that is written in Angular 2. In the method we have a call to to observable method after that we are assigning a value to a variable.我是 Angular 的新手,我正在努力修复用 Angular 2 编写的东西。在该方法中,我们调用了可观察方法,之后我们为变量赋值。 I need the obersavable method to finish execution before assigning the value.在分配值之前,我需要 obersavable 方法来完成执行。 Given below is the code for your reference.下面给出的代码供您参考。

Method1(): void {
     this.Service.getData()
        .subscribe(
        res => {
          
        },
        error =>  this.errorMessage = <any>error);
        this.gotData = true;
}

So, As shown above, I need to assign true to gotData variable after the getData Observable method is finished execution.因此,如上所示,我需要在 getData Observable 方法执行完毕后将 true 分配给 gotData 变量。 But gotData is getting assigned true even before we actually get the data.但是在我们实际获取数据之前,gotData 就被赋值为 true。

Please help me who to wait until the observable method is finished execution.请帮助我等到可观察方法完成执行。 Thank you !谢谢 !

Sounds like you need to read up on Observables and Subscriptions.听起来你需要阅读 Observables 和 Subscriptions。 Angular Docs Angular文档

Alternatively, the subscribe() method can accept callback function definitions in line, for next, error, and complete handlers.或者,subscribe() 方法可以在行中接受回调 function 定义,用于下一个、错误和完成处理程序。 For example, the following subscribe() call is the same as the one that specifies the predefined observer:例如,以下 subscribe() 调用与指定预定义观察者的调用相同:

In your case the callback you have named res will be called after your observable method is finished.在您的情况下,您命名为res的回调将在您的可观察方法完成后被调用。

So your code could be:所以你的代码可能是:

Method1(): void {
     this.Service.getData()
        .subscribe(
        res => {
          this.gotData = true;
        },
        error =>  this.errorMessage = <any>error
       );

}

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

相关问题 如何在继续之前等待graphQL中的查询完成? - How to wait for query in graphQL to finish before proceeding? 等待异步功能完成,然后再继续 - Wait for asynchronous function to finish before proceeding 等待先前的提取完成中止,然后再继续 - Wait for prior fetch to finish aborting before proceeding 等待AJAX​​完成后再继续循环吗? - Wait for AJAX to finish before proceeding with the loop? 使用酶进行 JS 单元测试:如何在继续测试用例之前等待组件方法中的 setState 完成 - JS unit testing using enzyme: how to wait for setState within component's method to finish before proceeding with test case 在 Angular 中继续之前如何等待来自服务的新数据 - How to wait for new data from service before proceeding in Angular 如何使角度等待函数在继续之前返回值 - How to make angular wait for a function to return a value before proceeding 如何在JS(节点)中使用回调来等待Async函数完成才能继续? - How to use callback in JS (node) to wait for Async function to finish before proceeding? 使用JQuery,如何在继续进行之前等待AJAX​​调用及其回调完成? - Using JQuery, how to wait for both an AJAX call and its callback to finish before proceeding? NodeJS:如何在继续执行代码之前等待异步请求的 for 循环完成? - NodeJS: How to wait for a for-loop of async requests to finish before proceeding with code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM