Angular 6 - 动态填充表格

kon*_*ban 0 html-table ngfor angular6

我正在尝试动态填充表格,但无法弄清楚为什么这不起作用!

在这里,我得到了我的 json,我将对象数组保存在 customerArray 中。

export class AllCustomersComponent implements OnInit {
customerArray: any;


    constructor(private http : HttpClient ) {}

    ngOnInit() {
         this.http.get('http://www.mocky.io/v2/5be715ce2e00008a0016945e')
        .subscribe((data) => {
           this.customerArray = data;
          console.log(customerArray);
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我试图用上述数组填充表格。

<table class="table">
        <thead>
          <tr>
            <th scope="col">First</th>
            <th scope="col">Last</th>
            <th scope="col">Age</th>
            <th scope="col">Edit</th>
            <th scope="col">Delete</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Mark</td>
            <td>Otto</td>
            <td>46</td>
            <td><i class="fas fa-edit"></i></td>
            <td><i class="fas fa-trash-alt"></i></td>
          </tr>
          <tr>
            <td *ngFor="let customer of customerArray">{{customer}}</td>
          </tr>
        </tbody>
      </table>
Run Code Online (Sandbox Code Playgroud)

但我的表只是呈现这个:

[object Object] [object Object] [object Object] [object Object] [object Object]
Run Code Online (Sandbox Code Playgroud)

Axe*_*l M 5

您的客户是一个对象,angular 应该如何知道要为其渲染什么?

你可能需要的是这样的:

<tr *ngFor="let customer of customerArray">
   <td>{{customer.firstname}}</td>
   <td>{{customer.lastname}}</td>
   <td>{{customer.age}}</td>
   <td>{{customer.whatever}}</td>
   <td>{{customer.whatever}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)