【问题标题】:Angular Material tables doesnt display data角材料表不显示数据
【发布时间】:2021-01-11 16:04:17
【问题描述】:

我已经在服务器中部署了我的 Angular 应用程序。经过多次测试,我发现Angular Material表没有显示任何数据,而您在日志中看不到数据。

我只是在生产中遇到这个问题,在开发中一切都很好。

数据将从数据库发送。

Table Image

可以清楚地看到,有数据但没有显示出来。

我的 HTML 文件:

<table mat-table [dataSource]="dataSource">
              <ng-container matColumnDef="CODE">
                <th id ="n" mat-header-cell *matHeaderCellDef>Code</th>
                <td mat-cell *matCellDef="let element">{{element.CODE}}</td>
              </ng-container>
              <ng-container matColumnDef="NAME">
                <th id="typeLien" mat-header-cell *matHeaderCellDef>Nom</th>
                <td mat-cell *matCellDef="let element">{{element.NAME}}</td>
              </ng-container>
              <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
              <tr mat-row *matRowDef="let element; columns: displayedColumns;"></tr>
            </table>

我的 TS 文件:

this.communicationService.getArticles().toPromise().then(allArticles=> {
      this.dataSource = new MatTableDataSource(allArticles);
      this.dataSource.paginator = this.paginator;
    });

【问题讨论】:

    标签: angular typescript angular-material mat-table


    【解决方案1】:

    这是因为表格在服务响应之前渲染。在渲染之前添加 *ngIf 条件。

    <table *ngIf="dataSource" mat-table [dataSource]="dataSource">
                  <ng-container matColumnDef="CODE">
                    <th id ="n" mat-header-cell *matHeaderCellDef>Code</th>
                    <td mat-cell *matCellDef="let element">{{element.CODE}}</td>
                  </ng-container>
                  <ng-container matColumnDef="NAME">
                    <th id="typeLien" mat-header-cell *matHeaderCellDef>Nom</th>
                    <td mat-cell *matCellDef="let element">{{element.NAME}}</td>
                  </ng-container>
                  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
                  <tr mat-row *matRowDef="let element; columns: displayedColumns;"></tr>
                </table>
    

    【讨论】:

    • 还是不行,看来我需要更改页面或页面大小才能获取元素
    【解决方案2】:

    你必须分配dataSource.data。试试这个:

    this.communicationService.getArticles().toPromise().then(allArticles=> {
          this.dataSource = new MatTableDataSource(allArticles);
          this.dataSource.data = allArticles;
          this.dataSource.paginator = this.paginator;
        });
    

    【讨论】:

    • 感谢您的回复,但我的问题是,为什么它在开发中正常工作,而在生产中没有?
    • 是的。除了部署我的应用外,我什么都没做
    【解决方案3】:
    this.communicationService.getArticles().toPromise().then(allArticles => {
        this.dataSource = new MatTableDataSource(allArticles);
        /*enter code here*/
        this.datasource.data = allArticles
        this.dataSource.paginator = this.paginator;
    });
    

    请像这样改变你的 Ts 面。

    【讨论】:

    • 还是不行,看来我需要更改页面或页面大小才能获取元素
    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2019-11-26
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多