Mat Table未在ngOnIt上显示数据,但在刷新其显示后 [英] Mat Table is not displaying data on ngOnIt but after refresh its displaying

查看:73
本文介绍了Mat Table未在ngOnIt上显示数据,但在刷新其显示后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Angular Material与Angular一起使用.

I'm using Angular Material with Angular.

我的表未在ngOnInit生命周期挂钩上显示数据,但是在刷新应用程序后,它显示:

My table does not display data on the ngOnInit lifecycle hook, but after I refresh the app it displays:

即使ChangeDetectorRef也不影响表格.

这是表格的模板:

<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource"> 
    <ng-container matColumnDef="Phone">
        <mat-header-cell *matHeaderCellDef> Phone </mat-header-cell>
        <mat-cell *matCellDef="let user"> {{ user.phone }} </mat-cell>
    </ng-container>
    <ng-container matColumnDef="LastName">
        <mat-header-cell *matHeaderCellDef> LastName </mat-header-cell>
        <mat-cell   *matCellDef="let user" > {{ user.lastname}} </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let user; columns: displayedColumns;"></mat-row>
    </mat-table>
    <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>

这是组件的文件:

import { ChangeDetectorRef } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { UserService } from './user.service';

export class UsersComponent implements OnInit {
    constructor(private ref: ChangeDetectorRef, private userService: UserService){}
    displayedColumns = ['ID', 'FirstName', 'Phone', 'LastName'];
    dataSource: MatTableDataSource<User>;
    users: User[] = [];
    ngOnInit() {
        this.users= this.userService.getUserList();
        this.dataSource = new MatTableDataSource(this.users);
        this.ref.detectChanges();
    }

以下是UserService的摘录,用于获取用户列表:

Here is a snippet of UserService for getting a user list:

getUserList() {
    const headers = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.authenticationService.token});
    const options = new RequestOptions({ headers: headers });
    this.http.get('/api/getallusers/', options)
     .map((data: Response) => {
         return data.json() as User[];
      }).toPromise().then(x => {
         this.userList = x;
      });
      return this.userList;
    }

推荐答案

This issue was there with the beta version though and that could be resolved using detect changes.

在您的情况下,真正的问题是没有在ngOnit中分配数据,您需要使用Observables而不是Promise并在组件内部进行订阅,按如下所示更改服务,

In your case , the real issue is data is not getting assigned in ngOnit, you need to use Observables instead of Promise and subscribe inside the component, Change your service as follows,

 getPatients() {
    const headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.authenticationService.token });
    const options = new RequestOptions({ headers: headers });
    return this.http.get('/api/getallpatients/', options)
      .map((response: Response) => response.json());
  }

并在组件内部调用它,

 ngOnInit() {
    this.patientService.getPatients().subscribe((data) => {
      this.patients = data;
      console.log('#####patients inside ngonit', this.patients);
      this.dataSource = new MatTableDataSource(this.patients);
    });

这篇关于Mat Table未在ngOnIt上显示数据,但在刷新其显示后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆