【问题标题】:Show the size according to the product in the cart in Ionic App在 Ionic App 中根据购物车中的产品显示尺寸
【发布时间】:2019-01-23 17:53:33
【问题描述】:

我正在开发 Ionic 电子商务应用程序,我在购物车中展示了具有尺寸的产品,有些产品有尺寸,有些没有尺寸,但问题是没有尺寸的产品正在显示购物车中的尺码标签。

这是我的 cart.html

<p *ngIf="ifSize">Size: {{itm?.psize}}</p>

在这个视图中,我显示的是产品的尺寸。

这是我的 cart.ts

import { CheckoutPage } from './../checkout/checkout';
import { Component, ChangeDetectorRef } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController, AlertController } from 'ionic-angular';
import { CartProvider } from "../../providers/cart/cart";

@IonicPage()
@Component({
  selector: 'page-cart',
  templateUrl: 'cart.html',
})
export class CartPage {
 cartItems: any[] = [];
 totalAmount: number = 0;
 isCartItemLoaded: boolean = false;
 isEmptyCart: boolean = true;
 ifSize: boolean = true;
 productCount: number = 1;
  constructor(public navCtrl: NavController, public navParams: NavParams, private cartService: CartProvider, public loadingCtrl: LoadingController, private alertCtrl: AlertController, private cdr: ChangeDetectorRef) {
  }

  ionViewDidLoad() {
    this.loadCartItems();
  }

  loadCartItems() {
    let loader = this.loadingCtrl.create({
      content: "Wait.."
    });
    loader.present();
    this.cartService
      .getCartItems()
      .then(val => {
        this.cartItems = val;
        console.log(val);
        if(val.psize === undefined)
        {
        this.ifSize = false;
        }
        if (this.cartItems.length > 0) {
          this.cartItems.forEach((v, indx) => {
            this.totalAmount += parseInt(v.totalPrice);
          });
          this.cdr.detectChanges();
          this.isEmptyCart = false;
        }
        this.isCartItemLoaded = true;
        loader.dismiss();
      })
      .catch(err => {});
  }
}

在这个 ts 文件中,默认情况下它会显示所有产品的尺寸以及没有尺寸标签将隐藏的尺寸的产品。但问题是 console.log(val);就像一系列产品一样即将到来。这就是为什么我无法在数组中找到产品的大小,也无法对产品数组中的大小应用正确的条件。任何帮助深表感谢。 结果:

console.log(val);

【问题讨论】:

  • 如果大小未定义时打印什么
  • @sunielkalwani。最初是真的。 ifSize: boolean = true;之后,如果它未定义。这将是错误的。
  • 只需使用

    Size: {{itm.psize}}

    。你不需要为同一个工作有两个变量
  • @Eliseo。谢谢你的回答。

标签: angular ionic-framework ionic3


【解决方案1】:

val.psize 在您的情况下不会返回任何内容,因为您需要 val[0].psize, val[1].psize 等等...要解决此问题,请删除此问题:

 if(val.psize === undefined)
        {
         this.ifSize = false;
        }

在此处添加 v.psize 并像这样检查 foreach 内部:

if (this.cartItems.length > 0) {
          this.cartItems.forEach((v, indx) => {

    //---Add here 
            if(v.psize===undefined){
              this.ifSize = false;
            }else{
              this.ifSize = true;
            }
    //---
            this.totalAmount += parseInt(v.totalPrice);
          });
          this.cdr.detectChanges();
          this.isEmptyCart = false;
        }

那么您的代码应该可以按预期工作

编辑: 也许你在看这个,我有点困惑:

<p *ngIf="itm?.psize!== undefined">Size: {{itm?.psize}}</p>

【讨论】:

  • 这是显示产品尺寸,但无法隐藏产品的尺寸标签。
  • 我不确定你的意思是什么?你也可以上传完整的html吗?因为在这里您显示itm?.psize,这可能意味着产品尺寸。您是否尝试了代码,因为您的 val.psize 没有任何意义?
  • 大小:{{itm?.psize}}

    。在这种情况下,它会获取所有产品的最终 ifSize 值,例如,如果添加的最后一个产品的尺寸未定义,那么它将隐藏购物车中所有产品的尺寸标签。
  • 你在ts上哪里定义itm?.psize
  • 如果一种产品有尺码而另一种没有尺码,我会遇到问题,因此它将 ifSize 设置为 false 并且所有产品尺码标签都将隐藏。那么,我们可以根据产品定义ifSize吗?
猜你喜欢
  • 2013-02-19
  • 2020-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多