angular - 使用Angular显示外键值而不是键
问题描述
我创建了两 (2) 个表 1. 服务:名称,service_type(来自 service_type 的 ID) 2. service_type:id,名称
我正在使用 Angular7 从 Laravel API 获取端点。我想从 service_type 表中显示名称(值而不是键)。services 是主表。
我已经创建了 model.ts、service.ts component.ts 和 component.html
模型
export class Services {
_id: string;
name: string;
service_type: number;
}
服务
getServices (): Observable<Services[]> {
return this.http.get<Services[]>(apiUrl)
.pipe(
tap(services => console.log('Fetch services')),
catchError(this.handleError('getServices', []))
);
}
getService(id: number): Observable<Services> {
const url = `${apiUrl}/${id}`;
return this.http.get<Services>(url).pipe(
tap(_ => console.log(`fetched service id=${id}`)),
catchError(this.handleError<Services>(`getService id=${id}`))
);
}
组件.ts
displayedColumns: string[] = ['name', 'service_type'];
data: Services[] = [];
isLoadingResults = true;
constructor(private api: ServicesService) { }
ngOnInit() {
this.api.getServices()
.subscribe(res => {
this.data = res;
console.log(this.data);
this.isLoadingResults = false;
}, err => {
console.log(err);
this.isLoadingResults = false;
});
组件.html
<tr *ngFor="let datas of data">
<td>{{datas.name}}</td>
<td>{{datas.service_type}}</td>
<td>
在
{{datas.service_type}}
它来自另一个名为 service_type 的表,我想显示值(名称)而不是键(id)
解决方案
推荐阅读
- jpeg2000 - jpeg2000解码器的最低内存要求是多少?
- sql-server - 存储过程用于显示表变量中的错误在另一个存储过程中不起作用
- javascript - Ajax返回多个输出?
- c# - Unity,重新加载场景后保持变量的值
- scala - Scala调用多个期货 - 可用时使用队列
- python-3.x - 如何访问
旁边的标签
最后声明:
- javascript - 任何人都可以为 victor 仓库问题提供 js 或 php 解决方案吗?
- sql - 如何在 Athena (Presto) 中使用上个月的每月间隔?
- asp.net - 在服务器内同时使用 WordPress 和 ASP.NET Framework
- javascript - 努力从 discord.js 中删除特定用户的反应。说删除未定义