2017-06-21 85 views
0

我從服務器的響應是這樣的:如何隱藏JSON對象(angular2)?

{ 
    "message": " list received successfully", 
    "success": "true", 
    "data": { 
    "_list": [{ 
     "id": "", 
     "name": "", 
     "image_url": null, 
     "is_occupation": true 
     }, 
     { 
     "id": "", 
     "name": "", 
     "image_url": null, 
     "is_occupation": true 
     } 
    ], 
    "responseCode":"", 
    } 

如何在angular2列表(HTML)顯示此數據?

+0

可能的重複[如何在Angular 2中使用TypeScript將對象正確轉換爲JSON](https://stackoverflow.com/questions/38372134/how-to-convert-an-object-to-json -correctly-in-angular-2-with-typecript) – Whisher

+0

他不想將其轉換(標題具有誤導性),而只是將列表中的對象屬性打印出來,據我所知 – Raven

回答

0
@Component({ 
selector:'list', 
template: ` 
<table> 
    <thead> 
     <th>id</th> 
     <th>name</th> 
     <th>image_url</th> 
     <th>is_occupation</th> 
    </thead> 
    <tbody> 
     <tr *ngFor="let data of responseData.data._list"> 
      <td>{{data.id}}</td> 
      <td>{{data.name}}</td> 
      <td>{{data.image_url}}</td> 
      <td>{{data.is_occupation}}</td> 
     </tr> 
    </tbody> 
</table>` 
}) 
export class List { 

    const responseData={ 
    "message": " list received successfully", 
    "success": "true", 
    "data": { 
    "_list": [ 
    { 
    "id": "", 
    "name": "", 
    "image_url": null, 
    "is_occupation": true 
    }, 
    { 
    "id": "", 
    "name": "", 
    "image_url": null, 
    "is_occupation": true 
    }], 
    "responseCode":"", 
    }; 

}