gpt4 book ai didi

angular - 垫表呈现行但不呈现数据

转载 作者:行者123 更新时间:2023-12-05 08:16:20 25 4
gpt4 key购买 nike

我在我的项目中使用 Angular 5 和 Material mat-table。

在我的组件文件中,我使用 observable 从服务中检索数据并将其绑定(bind)到组件上的变量。 Mat-table 显示行但不打印数据。我试过 ngFor,它似乎有效。控制台中没有错误。为什么表格没有显示在数据中有什么想法吗?

TS:

import { Component, OnInit, ChangeDetectorRef } from '@angular/core';

import { NotificationService } from './../../core/services/notification.service';
import { OperationConfiguration } from './../../core/models/operationConfiguration';
import { ProgressBarService } from '../../core/services/progress-bar.service';
import { OperationConfigurationService } from '../../core/services/operation-configuration.service';

@Component({
selector: 'app-operation-configuration-list',
templateUrl: './operation-configuration-list.component.html',
styleUrls: ['./operation-configuration-list.component.css']
})
export class OperationConfigurationListComponent implements OnInit {

operationConfigurations: OperationConfiguration[];
columnsToDisplay: ['id', 'columnName', 'hidden'];

constructor(private progressBarService: ProgressBarService,
private notificationService: NotificationService,
private configurationService: OperationConfigurationService) {
this.progressBarService.show();
}

ngOnInit() {
this.loadOperationConfigurations();
}

private loadOperationConfigurations(): void {

this.configurationService.getAll(null)
.subscribe(
results => {
this.operationConfigurations = results;
this.progressBarService.hide();
},
error => {
if (error.statusText === 'Unknown Error') {
this.notificationService.openSnackBar('An error occurred, please try again later.');
} else {
this.notificationService.openSnackBar(error.error);
}
this.progressBarService.hide();
}
);
}

}

HTML:

<!-- THIS WORKS -->
<ul>
<li *ngFor="let c of operationConfigurations">
{{ c.id }}
</li>
</ul>

<!-- THIS DOES NOT WORK -->
<mat-table [dataSource]="operationConfigurations">

<mat-header-row *matHeaderRowDef="columnsToDisplay"></mat-header-row>
<mat-row *matRowDef="let rowData; columns: columnsToDisplay"></mat-row>

<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> # </mat-header-cell>
<mat-cell *matCellDef="let column">
{{column.id}}
</mat-cell>
</ng-container>

<ng-container matColumnDef="columnName">
<mat-header-cell *matHeaderCellDef> Column </mat-header-cell>
<mat-cell *matCellDef="let column">
{{column.columnName}}
</mat-cell>
</ng-container>

<ng-container matColumnDef="hidden">
<mat-header-cell *matHeaderCellDef> Visibility </mat-header-cell>
<mat-cell *matCellDef="let column">
{{column.hidden}}
</mat-cell>
</ng-container>

</mat-table>

结果:

enter image description here

最佳答案

罪魁祸首似乎是我声明 columnsToDisplay

的方式

我更改了以下行:

columnsToDisplay: ['id', 'columnName', 'hidden'];

columnsToDisplay = ['id', 'columnName', 'hidden'];

问题就解决了。

仍在学习 typescript !

关于angular - 垫表呈现行但不呈现数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49795400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com