angular - Angular ngFor 无法显示数据
问题描述
我是 Angular 的新手,我已经阅读了很多关于我的问题的讨论,但没有找到解决方案。我的前面正在为我的 ngFor 生成一个。
我可以在控制台中看到我从我的 API 获取数据。有人可以帮助我吗?
这是我的 component.ts
import { Component } from '@angular/core';
import { environment } from '../../../environments/environment';
import { BasicRequestsService } from '../services/basic-requests.service';
import { Observable, concat } from 'rxjs';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
})
export class HomeComponent {
env = environment;
usersList;
constructor(
private basicRequestsService: BasicRequestsService,
) { }
ngOnInit() {
const args = new Array();
concat(
this.getUsersData(),
)
.subscribe();
}
getUsersData(): Observable<any> {
return Observable.create(observer => {
this.basicRequestsService.get('/users')
.subscribe(data => {
this.usersList = data;
console.log(this.usersList);
});
});
}
}
这是我的 HTML
<table class="table table-sm table-hover">
<thead class="thead-dark">
<th>Client</th>
</thead>
<tbody>
<tr *ngFor="let userList of usersList">
<td>{{ userList.firstName }}</td>
</tr>
</tbody>
</table>
我正在从我的 API 加载数据。
解决方案
试试下面的,
<table class="table table-sm table-hover">
<thead class="thead-dark">
<tr>
<th>Client</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let userList of usersList">
<td>{{ userList?.firstName }}</td>
</tr>
</tbody>
</table>
推荐阅读
- android - Android 重复键未定义样式 Theme_Transparent = 0x7f130217
- webgl - 将纹理元素添加到 webGL 中已纹理对象的最佳方法
- python - 我可以创建一个“多窗口”命令行 Python 程序吗?
- javascript - 为什么模板中的模态弹出窗口只显示在某些页面上?
- python - 在 Windows 10 上创建 virtualenv 时出错
- regex - 使用“替换”进行清理无法按预期工作 - Pandas
- c++ - 如何找到数组中相对质数的对?
- xml - 在没有databricks的pyspark中将RDD转换为xml
- swift - 如何在不阅读文档的情况下预先获取文档数量 - Firestore - Swift
- javascript - 使用市场应用程序结帐页面反应错误