Angular2. How can I check if an observable is completed?

后端 未结 6 536
轮回少年
轮回少年 2020-12-09 08:39

In my page there is a button that generates a report. That report needs data that is loaded using a http call to a rest endpoint when the page is loaded, but I do not have a

6条回答
  •  庸人自扰
    2020-12-09 09:14

    You can do this by using onCompleted callback in subscription. For example, let's say you show loading bar when user press report button;

    loadCompanies(): void {
         this._companyService.getCompanies().subscribe(
              response => {
                   this.companiesModel = response;
              },
              err => {
                   console.log(err);
                   //closeLoadingBar();
              },
              () => {
                   //do whatever you want
                   //closeLoadingBar()
              }
         )
    }
    
    generateReport() {
        //showLoadingBar()
        this.loadCompanies();
    }
    

    If you get error from your http call, onCompleted method will not be invoked, only onError will be invoked. If it is successful, onCompleted method will be called after your onNext method.

    Here is the documentation for subscribe. I hope it helps!

提交回复
热议问题