angular - 角度列表不显示数据
问题描述
我正在尝试使用 Angular 和 Spring Boot 制作一个 CRUD 应用程序。我试图创建一个客户列表。但是 GET 请求列表之后仍然是空的。
客户服务.ts:
export class CustomerService {
formData : Customer;
list : Customer[];
readonly rootURL ='http://localhost:8085/api/auth'
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
constructor(private http : HttpClient) { }
showList():Observable<Customer[]>{
return this.http.get<Customer[]>(this.rootURL+'/customer',
{headers : new HttpHeaders({ 'Content-Type': 'application/json' })})
.pipe(catchError(this.handleError))
}
}
客户列表.component.ts
export class CustomerListComponent implements OnInit {
constructor(
public httpClientService:CustomerService,
) { }
customer : Customer[] = [];
ngOnInit() {
this.httpClientService.showList().subscribe((data: Customer[])=>{
console.log(data);
this.customer = data;
})
}
populateForm(customer:Customer){
this.httpClientService.formData=Object.assign({}, customer);
}
}
客户列表.component.html
<tr *ngFor="let cus of httpClientService.list">
<td (click)="populateForm(cus)">{{cus.id}}</td>
<td (click)="populateForm(cus)">{{cus.firstname}} {{cus.lastname}}</td>
<td (click)="populateForm(cus)">{{cus.email}}</td>
<td><button (click)="onDelete(cus.id)" class="btn btn-sm btn-outline-danger">X</button></td>
enter image description here</tr>
解决方案
看来,您的项目中有两个不同的列表。
在您customer-list.component.html
尝试迭代本地客户列表的过程中,如下所示:
<tr *ngFor="let cus of customer">
<td (click)="populateForm(cus)">{{cus.id}}</td>
<td (click)="populateForm(cus)">{{cus.firstname}} {{cus.lastname}}</td>
<td (click)="populateForm(cus)">{{cus.email}}</td>
<td><button (click)="onDelete(cus.id)" class="btn btn-sm btn-outline-danger">X</button></td>
enter image description here</tr>
推荐阅读
- android - Google Play 控制台 - 托管的 Google Play (B2B) - 未分发的已关闭曲目
- flutter - flutter - iphone - 相机翻转图像
- python - 没有得到整个
- 使用 BeautifulSoup 的线
- django - 未找到没有参数的“user_logout”反向
- django - Django methodSerializer 不返回绝对图像 url
- flutter - 如何在颤动中选择默认单选按钮
- azure - 缺少必填字段时,是否可以强制 Azure API 管理返回 400 而不是 404?
- python - 在 numpy 数组中包含不同形状的信息的最佳方法?
- ruby-on-rails - Rails 18n French:使用带重音的单词时出现特殊字符
- java - 如何迭代思想 JLabel 对象以将图标添加到特定对象