我正在集成angular material和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 interface User { name: string; age: number; city: string; }
这是与表关联的组件:
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() {
}
}
最后,这里是噩梦。调用gapi并返回数据的服务。我可以确保api响应,尤其是这一行代码
回答后果用户[0]。名称全名
与具有值的字符串对象相对应。
export class UserService {
private serviceUrl = environment.apiUrl + '/getcust/';
private apiLoaded = false;
constructor(private http: HttpClient) { }
getUsers(): Observable<User[]> {
/* // THIS WORKS!!!
const u: User = {
'name': 'ss',
'city': 'città ',
'age': 3
};
return Observable.create(observable => {
observable.next([u]);
observable.complete();
});
*/
return Observable.create(observable => { // THIS DOESN'T WORK
return gapi.client.directory.users.list({
'customer': 'my_customer',
'maxResults': 1,
'orderBy': 'email'
}).then(response => {
const u: User = {
'name': response.result.users[0].name.fullName,
'city': 'citta',
'age': 3
};
observable.next([u]);
observable.complete();
});
});
}
}
正如您在评论中看到的,被评论的行有效,其他行无效。
Result with empty cells
Result with populated cells, without calling GAPI