0

i have a method function that create object. here my function

packetdata:any[];

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

if i use console log this.packet it result object like this

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

i try to put that each value on my table like this

<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>

but it return error Error Cannot read property.
How to resolve this?.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
dedypuji
  • 105
  • 1
  • 3
  • 12

3 Answers3

1

try to replace the ngFor to <td> like this :

<tr>
    <th > Packet Name </th>
    <td *ngFor="let item of packetdata">{{item.packetname}}</td>
</tr>
Mohamed Ali RACHID
  • 3,245
  • 11
  • 22
0
<th *ngFor="let item of packetdata"> Packet Name </th>
<td>{{item.packetname}}</td>

can't work

item is only available within <th> (the element that has the *ngFor directive applied) Packet Name {{item.packetname}}

You might want

<ng-container *ngFor="let item of packetdata">
  <th> Packet Name </th>
  <td>{{item.packetname}}</td>
</ng-container>
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

Add *ngIf="packetdata" to tag table and remove return inside de subscribe.

alehn96
  • 1,363
  • 1
  • 13
  • 23