【问题标题】:How to get a value from an Object that comes from a ManyToOne relationship in Angular CLI?如何从Angular CLI中来自ManyToOne关系的对象中获取值?
【发布时间】:2019-08-05 03:42:17
【问题描述】:

我正在使用 Spring Boot、Angular CLI 和 mySQL。

我有一个员工,但不能有一个婚姻状态,一个 M 状态可以在 N 个员工中。

在 localHost:8080 我得到正确的数组 json:

[{"id":1,"firstName":"Name","lastName":"surname1","emailAddress":"test@test1.com","status":{"statusId":1,"nameStatus":"Single"}

在我的角度表(localHost:4200)中,我得到了所有数据,但在状态列中我得到“[object Object]”。

每个人都有一个服务。

当我进行注册时,我有一个包含所有状态的下拉菜单,所以我可以得到它们。

这是我的 HTML 表格:

<table class="table table-bordered">
  <thead>
  <tr>
    <th *ngFor="let col of columns">{{col}}
    </th>
    <th>Actions</th>
  </tr>
  </thead>
  <tbody>
  <tr *ngFor="let employee of employees | paginate: {itemsPerPage: pageSize,
                                           currentPage: page,
                                           totalItems: employees.length}  | filterAll: searchString : field">
    <td *ngFor="let col of columns">{{employee[col]}}</td>
    <td>
      <button [ngClass]="getClassCondition(act.actionType)" *ngFor="let act of actions"
              (click)="actionFunc(act, employee)">{{act.label}}</button>
    </td>
  </tr>
  </tbody>
</table>

这里我有一个获取所有员工的 ngFor。

现在我还分享我的服务和我的 componets.ts:

status.service.ts:

@Injectable({
  providedIn: 'root'
})
export class StatusService {

  private baseUrl = 'http://localhost:8080/api';

  constructor(
    private http: HttpClient) { }

  getStatus(): Observable<Status[]> {
    return this.http.get<Status[]>(`${this.baseUrl}` + '/status');
  }
}

employee.service.ts

const httpOptions = {
  headers: new HttpHeaders({'Content-Type': 'application/json'})
};

@Injectable({
  providedIn: 'root'
})

export class EmployeeService {

  columns = COLUMNS;
  actions = ACTIONS;

  private baseUrl = 'http://localhost:8080/api/employees';

  constructor(private http: HttpClient) {
  }

  getColumns() {
    return this.columns;
  }

  getActions() {
    return this.actions;
  }

  getMetadata() {
    return this.metadata;
  }

  /** GET Employees from the server */
  getEmployees(): Observable<Employee[]> {
    return this.http.get<Employee[]>(this.baseUrl);
  }

  getEmployee(id: number): Observable<Employee> {
    const url = this.baseUrl + '/' + id;
    return this.http.get<Employee>(url);
  }

  /** PUT: update the employee on the server */
  updateEmployee(employee: Employee): Observable<any> {
    return this.http.put(`${this.baseUrl}/${employee.id}`, employee, httpOptions);
  }

  deleteEmployee(id: number): Observable<any> {
    return this.http.delete(`${this.baseUrl}/${id}`, {responseType: 'text'});
  }

}

这里我还有一个带有 COLUMNS 名称的 const。 员工.ts

export const COLUMNS = ['id', 'firstName', 'lastName', 'emailAddress', 'status'];

export class Employee {
  id: number;
  firstName: string;
  lastName: string;
  emailAddress: string;
  status: string;
}

status.ts

export class Status {
  statusId: number;
  nameStatus: string;
}

我必须做什么才能获得我的 status.Name? 有什么具体的吗?

如果您需要更多文档,请咨询我。

【问题讨论】:

  • 请把代码发给你.ts
  • @SachinGupta 已编辑
  • 您好,您可以添加.ts 中合并两个可观察对象的部分吗?谢谢。

标签: html angular spring-boot


【解决方案1】:

我怀疑 col 返回 status 并且 employee[col] 返回{"statusId":1,"nameStatus":"Single"}

所以错误似乎是正确的。

如果你只想显示 nameStatus,你可以在这里做一个解决方法:

<td *ngFor="let col of columns">{{employee[col]?.nameStatus ? employee[col]?.nameStatus : employee[col]}}</td>

【讨论】:

  • 好的,这是正确的解决方案,但是当我有一行没有状态时,它没有显示在表格中,并且我在控制台中收到此错误:错误类型错误:无法读取属性'nameStatus'未定义的
  • 正如@Stefan 建议的那样,您可以使用&lt;td&gt;{{employee?.status?.nameStatus}}&lt;/td&gt;,使用问号来避免空指针。我已经编辑了我的解决方案。
  • 我可以用ManyToMany做同样的事情吗?示例:我也有带名称和 ID 的技能,不同的服务,例如状态,在我的 employee.ts 中我添加了“skillList:Skill[];”。我怎样才能在我的表格中打印它?
  • 并非如此。这是一种解决方法,因为它仅在存在一个对象(状态)时才有效。对于多个对象,您将不得不选择不同的策略。
【解决方案2】:

您必须更具体,如何显示数据或将数据映射到您想要的结构

1) 而不是&lt;td *ngFor="let col of columns"&gt;{{employee[col]}}&lt;/td&gt; 你可以做

<td>{{employee.firstName}}</td>
<td>{{employee.lastName}}</td>
...
<td>{{employee.status.nameStatus}}</td>

2) 在绑定数据之前,将其映射到您想要的结构(在您的控制器/*.ts 文件中)。 看看Array map(

您的代码似乎不起作用,因为控制器从不分配员工方法

【讨论】:

  • 我知道这个解决方案,但我的表必须是动态的,所以如果我在我的服务中添加一列,我将能够只编辑组件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2021-04-23
  • 1970-01-01
相关资源
最近更新 更多