角度嵌套对象

时间:2021-06-13 05:33:27

标签: angular

我正在尝试显示嵌套在另一个对象中的对象属性。每个员工都有一个类别。我设法显示员工属性而不是 (define (combinations xss) (if (null? xss) '(()) (apply append (map (lambda (x) (map (lambda (ys) (cons x ys)) (combinations (cdr xss)))) (car xss))))) 感谢您的帮助 这是代码:

型号:

category.name

服务:

export interface Category{
id : number;
name: string;
    }

import {Category} from "./Category";

export interface Employee {
  id: number;
  name: string;
  email: string;
  jobTitle: string;
  phone: string;
  imageUrl: string;
  employeeCode: string;
  active: boolean;
  category : Category;
}

app.ts

  public getEmployees(): Observable<Employee[]> {
    return this.http.get<Employee[]>(`${this.apiServerUrl}/employee/all`);
  }

html,错误来自 {{employee?.category.name}}

public getEmployees(): void {
    this.employeeService.getEmployees().subscribe(
      (response: Employee[]) => {
        this.employees = response;
        console.log(this.employees);
      },
      (error: HttpErrorResponse) => {
        alert(error.message);
      }
    );
  }}

它从后端收到的一个对象数据的 JSON 示例:

<ul class="list-group list-group-flush">
          <li class="list-group-item"><i class="fa fa-envelope float-right"></i>{{employee?.email}}</li>
          <li class="list-group-item"><i class="fa fa-phone float-right"></i>Phone : {{employee?.phone}}</li>
          <li class="list-group-item"><i class="fa fa-briefcase float-right"></i>Active : {{employee?.active}}</li>
          <li class="list-group-item"><i class="fa fa-briefcase float-right"></i>Category : {{employee?.category.name}}</li>

1 个答案:

答案 0 :(得分:1)

听起来一个或多个对象没有类别。在这种情况下,这段代码可以解决这个问题

{{employee?.category?.name}}