2016-05-18 22 views
0

我不知道如何在* ngFor之前放置preloader(簡單圖像)。如果從服務器獲取數據,我的列表將出現,但只要數據未加載,圖片即會顯示。Angular 2 preloader


<tr ngFor='#product of products' >       
    <td >       
     {{product.id+ " " + product.title}} 
    </td>    
</tr> 

回答

0

你必須使用ngIf。這裏是一個模板,你可以使用

<table *ngIf="products.length != 0"> 
    <thead> 
     <tr> 
      <th>Id</th> 
      <th>Title</th> 
     </tr> 
    </thead> 
    <tbody> 

    <tr *ngFor="#product of products"> 
     <td>{{product.id}}</td> 
     <td>{{product.title}}</td> 
    </tr> 

    </tbody> 
</table> 

<loading-image *ngIf="products.length == 0"></loading-image> 

其中loading-image是你的一個組成部分,你自己定義

@Component({ 
    selector: 'loading-image', 
    directives: [], 
    template: ` 
    <img src="{{loadImg}}"> 
    ` 
}) 
export class LoadingImage { 
    loadImg = 'assets/img/your-loading-image.gif'; 
} 
+0

對不起我的角度是新的。什麼是這是新組件中選擇器的名稱?我可以在我的HTML模板中直接放置img標籤嗎? – Luger

+0

我編輯了我的答案。 是的,你可以改爲。但是,如果您在另一個組件中使用相同的加載映像,則可能需要爲其創建組件。 –