【问题标题】:Angular 6 - Populate table dynamicallyAngular 6 - 动态填充表格
【发布时间】:2018-11-11 15:05:53
【问题描述】:

我正在尝试动态填充表格,但不知道为什么这不起作用!

在这里我得到了我的 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);
      }
    }
}

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

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

但我的表只是呈现这个:

[object Object] [object Object] [object Object] [object Object] [object Object]

【问题讨论】:

  • console.log(customerArray); customerArray 在哪里定义?还是应该是this.customerArray

标签: html-table angular6 ngfor


【解决方案1】:

您的客户是一个对象,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>

【讨论】:

  • 这很有意义,现在正在运行!谢谢!
  • @kontenurban 如果它对您有用,您应该将答案标记为正确...
猜你喜欢
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 2019-07-13
  • 2023-03-10
  • 2012-09-16
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多