2017-06-13 58 views
1

我使用nodejs服務器從SQL數據庫中獲取數據。將Json數據從服務器存儲到Angular 2中的數組中

我將存儲在數據環節是環節存在的數組:

getTaches(): Observable<Tache[]> { 
    return this.http.get(this.tachesUrl) 
    .map(response => { 
     this.taches = response.json().data as Tache[]; 

     console.log(this.taches); 
    }) 
    .catch(this.handleError); 

} 

當我在控制檯上打印這個環節我得到結果爲:

undefined 

當我打印response.json( )我得到我的價值觀:

Object {taches: Array(8)} 

所以我刪除。數據,然後再試一次,然後我得到的TEMPL 1號線其他錯誤吃了文件:

ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed 

,這裏是我的html文件:

  <md-toolbar color="primary"> 
     <span>Taches non traitées</span> 
    </md-toolbar> 
     <md-card> 


      <md-list > 
         <ng-container *ngFor="let tache of this.tacheService.taches " > 

       <md-list-item *ngIf="tache.stat == 0" (click)="onSelect(tache)" [class.selectionnee]="tache === tacheSelectionnee">{{tache.stat}}+{{tache.id}} + {{tache.name}} 

        <div *ngIf="tache === tacheSelectionnee"> 

         <button md-icon-button [mdMenuTriggerFor]="menu"> 
          <md-icon>more_vert</md-icon> 
         </button> 
         <md-menu #menu="mdMenu"> 
          <button md-menu-item (click)="openDialog(tache)"> 
           <md-icon>edit</md-icon> 
           <span>Modifier</span> 
          </button> 
          <button md-menu-item (click)="delete(tache)"> 
           <md-icon>delete</md-icon> 
           <span>Supprimer</span> 
          </button> 
          <button md-menu-item (click)="save(tache)"> 
           <md-icon>cached</md-icon> 
           <span>Traiter</span> 
          </button> 
         </md-menu> 


        </div> 

       </md-list-item> 

       </ng-container> 

      </md-list> 
     </md-card> 

我會正確地存儲在陣列環節的數據,這樣我就可以向他們的待辦事項列表。

回答

1

試試這個:收到

getTaches(): Observable<Tache[]> { 
    return this.http.get(this.tachesUrl) 
     .map(response => { 
     this.taches = response.json().taches as Tache[]; 

    console.log(this.taches); 
}) 
    .catch(this.handleError); 
} 

你的對象從API持有taches陣列

+0

謝謝你,它的工作! –

相關問題