javascript - 离子(角度)从数组中获取正确的数据
问题描述
我有一个数据数组,其中包括每个项目内的数组,到目前为止,我可以显示我想要的确切数据,除了 id。
逻辑
- 在数组中显示数组
- 获取数组 id(不是数组 id 中的数组
explanation in screenshot
)
截屏
Information provided in image of what ID I need to have
代码
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']);
}
);
}
View
<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 中绑定它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>
推荐阅读
- reactjs - 如何将组件父亲的一个状态传递给组件子组件以打开模态 React
- python - 正确使用 PyObject_Realloc
- android - Android 设备未收到推送通知问题
- linux - Bash:从另一个脚本中找到一个脚本的父进程ID并采取一些行动
- xml - 使用 xsi:type 反序列化 xml 会导致类型无法识别错误。我的课程定义中缺少什么?
- javascript - 如何识别嵌套对象中的键和过滤器字符串
- amazon-web-services - 如果 LB 中没有添加 ec2,有什么方法可以在负载均衡器上设置维护页面
- html - 编译错误:BC30807:不支持赋值语句“Let”和“Set”
- javascript - React Hooks:在“位置”中获取数据,但不是在“链接”的参数中
- ssas - SSAS 多维数据集 - 有多少“节点”?