2017-06-14 102 views
0

我有一個創建對象的方法函數。我在這裏的功能Angular2錯誤無法讀取屬性

packetdata:any[]; 

ngOnInit() { 
    this.service.getonepacket(this.nopacket).subscribe(
     data => { 
      return this.packetdata = data; 
     }, 
     error => console.log(this.errorMessage = <any>error) 
    ) 
    } 

如果我使用控制檯日誌this.packet就會導致物體像這樣

Object {idpacket: 3, packetname: "Packet C", packetdesc: "SMS 20 sms/hari, Storage 20Gb", packetprize: "Rp. 300.000/bulan", packettime: 30} 

我試圖把那對我的表中的每個值這樣

<h3 class="page-header">Your Bill Information</h3> 
<table class="table table-striped"> 
    <tr> 
    <th *ngFor="let item of packetdata"> Packet Name </th> 
    <td>{{item.packetname}}</td> 
    </tr> 
    <tr> 
    <th *ngFor="let item of packetdata"> Description </th> 
    <td> {{item.packetdesc}} </td> 
    </tr> 
    <tr> 
    <th *ngFor="let item of packetdata"> Prize </th> 
    <td> {{item.packetprize}} </td> 
    </tr> 
    <tr> 
    <th *ngFor="let item of packetdata"> Minimal Contract time (day) </th> 
    <td> {{item.packettime}} </td> 
    </tr> 
</table> 

但它返回錯誤錯誤無法讀取屬性。
如何解決這個問題?

+0

請發佈確切的錯誤信息。 –

+0

像那個先生@GünterZöchbauer 錯誤:./BillComponent類中的錯誤BillComponent - 內聯模板:5:8引起的:無法讀取未定義的屬性'packetname' – dedypuji

回答

0
<th *ngFor="let item of packetdata"> Packet Name </th> 
<td>{{item.packetname}}</td> 

不能工作

item只有<th>(已申請了*ngFor指令元素) 包中可用名稱{{item.packetname}}

您可能需要

<ng-container *ngFor="let item of packetdata"> 
    <th> Packet Name </th> 
    <td>{{item.packetname}}</td> 
</ng-container> 
+0

我使用你的坐下來更改我的代碼。但它又出現錯誤 – dedypuji

+0

無法找到類型爲'object'的不同支持對象'[object Object]'。 NgFor僅支持與陣列等Iterables綁定。 – dedypuji

+0

查看https://stackoverflow.com/questions/41396435/how-to-iterate-object-object-using-ngfor-in-angular-2/41396558#41396558 –

1

嘗試更換ngFor到<td>這樣的:

<tr> 
    <th > Packet Name </th> 
    <td *ngFor="let item of packetdata">{{item.packetname}}</td> 
</tr> 
0

將* ngIf =「packetdata」添加到標記表並刪除de訂閱中的返回。

相關問題