首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >整合mat-table与observable和HTTP请求

整合mat-table与observable和HTTP请求
EN

Stack Overflow用户
提问于 2018-11-28 04:27:25
回答 1查看 658关注 0票数 0

嗨,我只想显示在mat-table的HTTP请求的结果。

我不想知道为什么我不能展示。

我的组件:

代码语言:javascript
复制
    displayedColumns: string[] = ['id'];
dataSource = new MatTableDataSource<User>();

constructor(private httpService: HttpService) {
  this.httpService.accounts.subscribe(users => { this.dataSource.data = users; });
}

ngOnInit() {
}

我的html:

代码语言:javascript
复制
<mat-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef>id</mat-header-cell>
<mat-cell *matCellDef="let user">{{user.id}}</mat-cell>>
</ng-container>

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

当我使用console.log时,我有属性id的所有用户。

EN

回答 1

Stack Overflow用户

发布于 2018-11-28 18:19:43

我认为每当数据源发生变化时,都需要调用_updateChangeSubscription()来更新视图。MatTable不会隐式接受更改。

代码语言:javascript
复制
displayedColumns: string[] = ['id'];
dataSource = new MatTableDataSource<User>();

constructor(private httpService: HttpService) {
  this.httpService.accounts.subscribe(users => {
    this.dataSource.data = users;
    this.dataSource._updateChangeSubscription();
  });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53507605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档