我想通过获取ID来显示属性的值并显示其值。
service.ts
GetCategory(id:number){
return this._http.get<Category>(`${environment.url}/category/${id}`);
}
category.model.ts
export class Category
{
categoryId: number;
categoryName: string;
latinName: string;
mainCategoryId: number | null;
notes: string;
}
component.ts
GetCategoryName(id:number){
if(id == null){
return "no main category";
}
else {
let category:Category;
return this._categoryService.GetCategory(id)
.subscribe(res=>{
category.categoryName = res.categoryName
},
error=> {
console.log("can't get");
},
)
}
}
component.html
<tr *ngFor="let item of categories; let i = index ">
<td>{{i+1}}</td>
<td>{{item.categoryName}}</td>
<td>{{item.latinName }}</td>
<td>{{GetCategoryName(item.mainCategoryId)}}</td>
<td>{{item.latinName}}</td>
<td>
浏览器冻结。