angular - Angular Material:表格不包含内容
问题描述
我目前正在开发一个基于“英雄之旅”教程的小型 Angular 项目。然而,“hero/heroes”被“customer/customers”取代,但一切都是一样的编程方式。
所以我有这个模板:
<!-- This part isn't working. -->
<table #table mat-table [dataSource]="customers" class="mat-elevation-z8">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let customer"> {{customer.id}} </td>
</ng-container>
<ng-container matColumnDef="surname">
<th mat-header-cell *matHeaderCellDef> Surname </th>
<td mat-cell *matCellDef="let customer"> {{customer.surname}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>
<!-- This part works - based on Tour of Heroes -->
<h2>Customers</h2>
<ul class="customers">
<li *ngFor="let customer of customers">
<a routerLink="/detail/{{customer.id}}">
<span class="badge">{{customer.id}}</span> {{customer.surname}} {{customer.lastname}}
</a>
<button class="delete" title="delete customer" (click)="delete(customer)">x</button>
</li>
</ul>
这是模板的组件:
import { Component, OnInit } from '@angular/core';
import { Customer } from '../models/customer';
import { CustomerService } from '../customer.service';
import { MatTable } from '@angular/material';
@Component({
selector: 'app-customers',
templateUrl: './customers.component.html',
styleUrls: ['./customers.component.css']
})
export class CustomersComponent implements OnInit {
customers: Customer[];
columnsToDisplay: ['ID', 'Surname'];
constructor(private customerService: CustomerService) { }
ngOnInit() {
this.getCustomers();
}
getCustomers(): void {
this.customerService.getCustomers().subscribe(customers => this.customers = customers);
}
delete(customer: Customer): void {
this.customers = this.customers.filter(c => c !== customer);
this.customerService.deleteCustomer(customer).subscribe();
}
}
Chrome 控制台不会显示任何错误。我还在 app.module.ts 文件中导入了 MatTableModule。
我的问题与这个问题类似,但我什至没有得到头条新闻,解决方案在这里没有帮助。
如果您需要更多信息,请告诉我。
解决方案
问题似乎是您对columnsToDisplay
(ID, Surname) 的定义与模板中由 (position, surname) 定义的列不匹配matColumnDef
,因此 Angular 无法找到/显示它们。
要么改变ts
:
columnsToDisplay = ['position', 'surname'];
或在HTML
:
<ng-container matColumnDef="ID">...</ng-container>
<ng-container matColumnDef="Surname">...</ng-container>
推荐阅读
- loops - 从函数/模块中提取 llvm 位码中的循环
- php - Laravel:试图获取非对象的属性“符号”
- angular - Angular 7 Reactive forms“具有未指定名称属性的表单控件没有值访问器”
- docker - kubeadm init [恐慌:分配给 nil 映射中的条目]
- angular - 将 CSS 类应用于组件标签
- python - Keras/Tensorflow中dropout的动态切换
- powershell - 如何调用 Jenkins API 并使用 powershell 发送自定义参数
- sql - 行聚合的案例语句
- javascript - 如何导入 Typescript 所需的 chrono 库?
- java - 当你在 mockito 框架中模拟一个类时,你会手动建立实际的模拟类吗?