这可能是由于Angular Material表格使用了CDK的虚拟滚动功能导致的。为了解决这个问题,可以使用ChangeDetectorRef来强制刷新组件。
例如,在组件中注入ChangeDetectorRef,然后在每次更新数据后手动调用它的detectChanges()方法:
import {ChangeDetectorRef, Component} from '@angular/core';
@Component({ selector: 'app-table', templateUrl: './table.component.html', styleUrls: ['./table.component.scss'] }) export class TableComponent { public dataSource = [];
constructor(private cdr: ChangeDetectorRef) {}
public loadData(): void {
// Code to load data
// ...
this.dataSource = newData;
this.cdr.detectChanges();
}
}
在这个示例中,loadData()方法是用来加载数据的,并且在更新数据后手动调用了detectChanges()方法。
这样修改后,您的数据应该将立即显示在表格中。