更新时间:2023-11-17 13:23:34
也许您根本不需要管道,而在组件中则需要服务
Maybe you dont need a pipe at all and you need a service ,in your component
originalProducts;
orderedProducts;
ngOnInit() {
this.getProducts();
}
getProducts() {
this.productsService.getProducts()
.subscribe((data) => {
this.originalProducts = data;
this.sortBy('price');
});
}
sortBy(field: string) {
this.originalProducts.sort((a: any, b: any) => {
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
} else {
return 0;
}
});
this.orderedProducts = this.originalProducts;
}
在您的模板中
<div *ngFor="let product of orderedProducts">
如果您的列表太长,请使用分页.
And if your list is too long then use pagination.
如果仍然遇到问题,请使用lodash.
If still you have trouble, use lodash.
祝你好运!