zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Angular] New async 'as' syntax and ngIf.. else

Angularasync and &# 39 New as ..
2023-09-14 08:59:18 时间

From Anuglar v4 above, we are able to using 'as' with async pipe. This allow as using 'new variable' instead of subscribe to observable.

We also able to use 'else' with '*ngIf', else taks a 'template' which will be displayed when the if expression is not matched.

      <ng-template #loading>
        <div class="message">
          <img src="/img/loading.svg" alt="loading...">
          Fetching meals...
        </div>
      </ng-template>
      
      <div *ngIf="meals$ | async as meals; else loading;">
         <div class="message" *ngIf="!meals.length">
           <img src="/img/face.svg" alt="no meals">
           No meals, add new meal to add
         </div>
        <!-- meal ngFor -->
      </div>