How to show a loading spinner while waiting on an observable getting data from a service in Angular

前端 未结 6 1417
情书的邮戳
情书的邮戳 2020-11-27 08:35

I have set up an observable service that helps me persist data, but I need a way to show a loading spinner while the data is waiting to come from the observable.

My

6条回答
  •  攒了一身酷
    2020-11-27 08:57

    Thank you all for your responses, but none of these suggestions worked for the purposes outlined in the question, but I did work on a solution and eventually got one working. Here we go:

    In the service I created a new Observable called loading$ and set it up as a new BehaviorSubject(true);

    Then I created a getter:

    getLoading(): Observable { return this.loading$; }

    Then I wrapped the HTTP call with a true then set loading to false in the 3rd argument on the subscription (the onCompleted function).

    So the service looks like this:

    import { Observable, BehaviorSubject, of } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class OrganisationsService {
      private organisations$ = new BehaviorSubject([
        {
          [...]
        }
      ]);
    
      //initiate the new Loading variable via BehaviorSubject and set it to "true" from the beginning.
      public loading$ = new BehaviorSubject(true);
    
      constructor(private http: HttpClient) {
    
        //set the loading$ to true again just before we start the HTTP call
        this.loading$.next(true);
    
    
        this.http
          .get(environment.apicall)
          .subscribe(
    
            //onNext method, where we get the data
            (data) => this.organisations$.next(data['organisations']),
    
            //onError method (Blank for now)
            () => {},
    
            //onComplated method, now that we have the data we can set the loading to false
            () => {
              this.loading$.next(false);
            }
          );
      }
    
      getLoading(): Observable {
        return this.loading$;
      }
    
      getList(): Observable {
        return this.organisations$;
      }
    

    Notice in the subscribe method on the HTTP call, I'm adding a blank second method (This is for onError) and then in the 3rd method, I've added a function to set the loading to false: this.loading$.next(false); . This means that Loading will now be false when the subscription is complete.

    Then in my component, I get the loading$ subscription:

          public loading$: Observable;
          public organisations$: Observable;
    
          constructor(public organisationsService: OrganisationsService) {}
    
          ngOnInit() {
            this.getData();
          }
    
          async getData() {
    
            //initially loading$ will be true because the service makes it so
            this.loading$ = this.organisationsService.getLoading();
    
            //when this completes, loading$ will be set to false via the service
            this.organisations$ = this.organisationsService.getList();
          }
    

    And in my view:

      
    • {{ organisation.name }}

提交回复
热议问题