Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角材料&x2B;GAPI:表格显示行,但单元格为空_Angular_Typescript_Angular Material_Google Api Js Client - Fatal编程技术网

Angular 角材料&x2B;GAPI:表格显示行,但单元格为空

Angular 角材料&x2B;GAPI:表格显示行,但单元格为空,angular,typescript,angular-material,google-api-js-client,Angular,Typescript,Angular Material,Google Api Js Client,我正在集成angular材质和Google api。我想在一个表中显示api的响应。问题是mat表格行的mat单元格未填充,但显示了该行。这是我的桌子: <mat-table [dataSource]="dataSource"> <ng-container matColumnDef="name"> <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>

我正在集成angular材质和Google api。我想在一个表中显示api的响应。问题是mat表格行的mat单元格未填充,但显示了该行。这是我的桌子:

    <mat-table [dataSource]="dataSource">

    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let user"> {{user.name}} </mat-cell>
    </ng-container>
    <ng-container matColumnDef="age">
      <mat-header-cell *matHeaderCellDef> Age </mat-header-cell>
      <mat-cell *matCellDef="let user"> {{user.age}} </mat-cell>
    </ng-container>
    <ng-container matColumnDef="city">
      <mat-header-cell *matHeaderCellDef> City </mat-header-cell>
      <mat-cell *matCellDef="let user"> {{user.city}} </mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>
这是与表关联的组件:

    export class UsertableComponent implements OnInit, OnChanges {
  @Input() apiReady: boolean;

  dataSource: UserDataSource;
  displayedColumns = ['name', 'age', 'city'];

  constructor(private userService: UserService) {

  }

  ngOnInit() {

  }

  ngOnChanges(changes: SimpleChanges) {
    if (this.apiReady) {
      this.dataSource = new UserDataSource(this.userService);
    }
  }
}

export class UserDataSource extends DataSource<any> {
  constructor(private userService: UserService) {
    super();
  }

  connect(): Observable<User[]> {
    return this.userService.getUsers();
  }

  disconnect() {

  }
}
导出类UsertableComponent实现OnInit、OnChanges{
@Input()apiReady:布尔值;
数据源:UserDataSource;
displayedColumns=['name','age','city'];
构造函数(私有用户服务:用户服务){
}
恩戈尼尼特(){
}
ngOnChanges(更改:SimpleChanges){
如果(此.apiday){
this.dataSource=新的UserDataSource(this.userService);
}
}
}
导出类UserDataSource扩展了DataSource

解决方案是:

导入此文件:

import {ChangeDetectorRef} from '@angular/core';
组件的构造函数:

constructor(private cd: ChangeDetectorRef) {
  }
在ngInit中,初始化数据源:

    ngOnInit() {
    this.dataSource = new UserDataSource();
}
使用BehaviorSubject(方法为可观察的)随时更改数据源的内容,但当表中的数据更改时,不要忘记调用:

this.cd.detectChanges()


要更新单元格内容

gapi.client.directory.users.list
返回什么?承诺?我想是的。这就是javascript的快速入门指南:这个问题展示了如何使用typescript进行操作:控制台中没有出现任何错误吗?在API调用之前,可以尝试不使用
返回
。关键是,当可观察到的内容完成时,行会出现在表中,但单元格不会填充。但是我放在next()方法中的对象是正确的。我也尝试过使用与示例完全相同的对象,但单元格仍然是空的:const u:User={'name':'ss','city':'cittá','age':3};已经试过了,什么都没有。
constructor(private cd: ChangeDetectorRef) {
  }
    ngOnInit() {
    this.dataSource = new UserDataSource();
}