如何通过HTTP请求中的ID获取属性名称?

时间:2019-09-03 11:15:22

标签: angular typescript

我想通过获取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>

浏览器冻结。

0 个答案:

没有答案