首页 > 解决方案 > 如何在 Angular 中显示列表

问题描述

我想在我的 Angular 前端显示工单列表。我的意思是在下面的图片上显示ticketIdplace喜欢。

这是我的请求http://localhost:8080/api/customers

在此处输入图像描述

这是结果http://localhost:4200/customer

不幸的是,门票清单是空的。我不知道如何展示它。

在此处输入图像描述

这是我的课customer-details.component.html

<div *ngIf="customer">
<div>
<label>Name: </label> {{customer.name}}
</div>
<div>
<label>Surname: </label> {{customer.surname}}
</div>
<div>
<label>List of tickets: </label> {{customer.tickets}}
</div>
<span class="button is-small btn-danger"  (click)='deleteCustomer()'>Delete</span>
<hr/>
</div>

我的课customers-list.component.html

<div *ngFor="let customer of customers | async" style="width: 300px;">
<customer-details [customer]='customer'></customer-details>
</div>

<div>
<button type="button" class="button btn-danger" (click)='deleteCustomers()'>Delete all</button>
</div>

班级customer.service.ts

export class CustomerService {

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

constructor(private http: HttpClient) {
}

getCustomersList(): Observable<any> {
return this.http.get(`${this.baseUrl}`);
}

我的课customer.ts

export class Customer {
customerId: number;
name: string;
surname: string;
tickets: Ticket[];
}
export class Ticket {
ticketId: number;
place: number;
customerId: number;
flightId: number;
}

班级customers-list.component.ts

export class CustomersListComponent implements OnInit {

customers: Observable<Customer[]>;

constructor(private customerService: CustomerService) { }

ngOnInit() {
this.reloadData();
}

reloadData() {
this.customers = this.customerService.getCustomersList();
}
}

标签: htmlangular

解决方案


在你的customer-details.component.html你必须迭代你的ticket.

你可以尝试这样的事情:

<div>
  <label>List of tickets: </label>
  <div *ngFor="let ticket of customer.ticket">
    {{ ticket.ticketId }}
    {{ ticket.place }}
  </div>
</div>

推荐阅读