Display loading while using Observable with Async pipe in template

后端 未结 3 934
感情败类
感情败类 2021-02-20 15:01

Situation: I am using FirebaseObjectObservable to populate my Ionic 2 (rc0) template. Template code:


  

{{(course | async)?.des

相关标签:
3条回答
  • 2021-02-20 15:42

    Lets say we have an observable meals$. We need to show loader when the observable is getting resolved(i.e fetching data). Below is the solution.

    <div *ngIf="meal$ | async as meal; else loading;" >
       //use the meal variable to show some data
    </div>
    <ng-template #loading>
       //Show your loader here
    </ng-template>
    
    0 讨论(0)
  • 2021-02-20 15:43

    You could do something like this:

    <style>
      pre {
       color: orange;
       // or whatever you want
      }
    </style>
    <ion-card-content>
      <p>{{(course | async)?.description}}</p>
      <br>
      <h2>Learning Objectives</h2>
      <pre *ngIf="!(course | async)">loading objectives...</pre>
      <ul>
        <li *ngFor = "let objective of (course | async)?.objectives">{{objective.text}}</li>
      </ul>
      <h2>Takeaway</h2>
      <pre *ngIf="!(course | async)">loading takeaways...</pre>
      <ul>
        <li *ngFor = "let takeaway of (course | async)?.takeaways">{{takeaway.text}}</li>
      </ul>
    </ion-card-content>
    
    0 讨论(0)
  • 2021-02-20 15:48

    Maybe a little late but in case someone else is wondering how to manage this... What about using a template?

    you could for example use something like:

    <ion-card-content *ngIf='(course$ | async) as course; else loading'>
      <p>{{course.description}}</p>
      <br>
      <h2>Learning Objectives</h2>
      <ul>
         <li *ngFor = "let objective of course.objectives"> 
             {{objective.text}}</li>
      </ul>
      <h2>Takeaway</h2>
      <ul>
         <li *ngFor = "let takeaway of course.takeaways"> 
             {{takeaway.text}}</li>
      </ul>
    </ion-card-content>
    
    <ng-template #loading>
      Loading stuff...
    </ng-template>
    

    so your ion-card-content will be hidden, showing the #template, until the async pipe is loaded.

    0 讨论(0)
提交回复
热议问题