javascript - Ionic(Angular)从数组中获取正确的数据

标签 javascript arrays angular ionic-framework

我有数据数组,其中每个项目内都包含数组,到目前为止,我可以显示除 id 之外的确切数据。

逻辑

  1. 在数组中显示数组
  2. 获取数组 ID(不是数组 ID 中的数组屏幕截图中的说明)

屏幕截图

图像中提供了我需要拥有的 ID 的信息

one

代码

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/

相关文章:

angular - primeng table p-table 列和浏览器窗口大小调整时的数据重叠问题

javascript - 如何使用 sequelize 从 sqlite 中的现有表中查询

javascript - 如何在动态元素上添加单击事件或鼠标悬停(进入/更新/退出)?

javascript - 如何将 dropEffect 光标定位为样式

c++ - 检查尺寸值及其内容的功能

javascript - 元素类测试显示 ng-untouched 和正确输入失败

angular - 如何在 Angular 6 中构建具有多个组件的表单?

javascript - 当您不知道会有多少嵌套 JSON 对象时,如何为每个级别生成一个 Vue 组件?

javascript - 如何在数组对象中插入元素

php - array_walk() 是做什么的?