我有数据数组,其中每个项目内都包含数组,到目前为止,我可以显示除 id 之外的确切数据。
逻辑
- 在数组中显示数组
- 获取数组 ID(不是数组 ID 中的数组
屏幕截图中的说明
)
屏幕截图
图像中提供了我需要拥有的 ID 的信息
代码
Controller
products: any[] = [];
limit = 10;
loading: any;
ngOnInit() {
this.getProducts();
}
async getProducts() {
this.loading = await this.loadingController.create({
message: 'Please wait...',
spinner: 'dots',
duration: 3000
});
await this.loading.present();
this.wishlistService.getProducts().subscribe((res) => {
console.log('res', res);
console.log('wishes', res['wishes']); // in screenshot you see this data
for (let wish of res['wishes']) {
this.products.push(wish.product);
}
this.hideLoading();
});
}
private hideLoading() {
this.loading.dismiss();
}
// For this function I need the upper ID (currently getting product ID I need wishlist item ID)
removeWishlist(id: string) {
console.log('product id for remove', id);
this.wishlistService.remove(id).subscribe(
data => {
this.alertService.presentToast(data['message']);
},
error => {
this.alertService.presentToast(error['message']);
}
);
}
查看
<ion-row *ngIf="products.length>0 else noItem">
<ion-col size="6" class="prodCard" size-sm *ngFor="let product of products | slice:0:limit">
<img [src]="product.photo">
<h4 class="productTitle">
{{product.name}}
</h4>
<ion-row>
<ion-col size="9" size-sm>
Prices here
</ion-col>
<ion-col size="3" size-sm class="ion-text-center">
<ion-icon (click)="removeWishlist(product.id)" color="danger" name="heart"></ion-icon>
</ion-col>
</ion-row>
</ion-col>
</ion-row>
有什么想法吗?
最佳答案
在显示迭代中,您可以添加一个属性以方便运行代码。尽管我建议您重新访问您的代码,但如果您可以更改 API,那就更好了。如果你不希望这有帮助
products: any[] = [];
limit = 10;
loading: any;
ngOnInit() {
this.getProducts();
}
async getProducts() {
this.loading = await this.loadingController.create({
message: 'Please wait...',
spinner: 'dots',
duration: 3000
});
await this.loading.present();
this.wishlistService.getProducts().subscribe((res) => {
console.log('res', res);
console.log('wishes', res['wishes']); // in screenshot you see this data
for (let wish of res['wishes']) {
wish.product.iconId = wish.id; // <------ added this
this.products.push(wish.product);
}
this.hideLoading();
});
}
private hideLoading() {
this.loading.dismiss();
}
// For this function I need the upper ID (currently getting product ID I need wishlist item ID)
removeWishlist(id: string) {
console.log('product id for remove', id);
this.wishlistService.remove(id).subscribe(
data => {
this.alertService.presentToast(data['message']);
},
error => {
this.alertService.presentToast(error['message']);
}
);
}
在你的html中绑定(bind)它iconId
<ion-row *ngIf="products.length>0 else noItem">
<ion-col size="6" class="prodCard" size-sm *ngFor="let product of products | slice:0:limit">
<img [src]="product.photo">
<h4 class="productTitle">
{{product.name}}
</h4>
<ion-row>
<ion-col size="9" size-sm>
Prices here
</ion-col>
<ion-col size="3" size-sm class="ion-text-center">
<ion-icon (click)="removeWishlist(product.iconId)" color="danger" name="heart"></ion-icon>
</ion-col>
</ion-row>
</ion-col>
</ion-row>
关于javascript - Ionic(Angular)从数组中获取正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57879797/