简体   繁体   中英

How to show list in Angular

I want to show the list of tickets in my Angular frontend. I mean to show ticketId and place like on the picture below.

Here is my request from http://localhost:8080/api/customers

在此处输入图片说明

Here is the result in http://localhost:4200/customer

Unfortunately list of tickets is empty. I don't know how to show it.

在此处输入图片说明

Here is my class 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>

My class 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>

Class 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}`);
}

My class customer.ts

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

Class 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();
}
}

In your customer-details.component.html you have to iterate on your ticket .

You can try something like this :

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

According to the picture of the json response the property is called ticket , not tickets . So you want to either rename it (since it's clearly a misnomer) or iterate accordingly:

*ngFor="let ticket of customer.ticket"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM