简体   繁体   English

我想在Ionic 3的购物车中获得所有产品价格的总和

[英]I want to get the sum total of all product prices in my cart in Ionic 3

This is my HTML 这是我的HTML

<ion-content padding>
  <ion-list *ngFor="let item of orderList$ | async">
    <ion-item>
      <ion-label text-wrap>
        <h2>{{item?.name}}</h2>
        <p  style="color: black">Quantity :  {{item?.qty}}</p>
         <p style="color: black">Price :  {{item?.price}}
         <p class="pr" style="font-weight: bold; color: black">Total :</p><p class="pr" style="color: red"> {{item?.total}}</p>
      </ion-label>
    </ion-item>
  </ion-list>

  <h1>Total Price Here</h1>
  <button ion-button block clear>Place Order</button>

This is my .ts 这是我的.ts

constructor(
public navCtrl: NavController, 
public navParams: NavParams,
private ord: OrderListService) {

  this.orderList$ = this.ord
  .getOrderList() // DB List
  .snapshotChanges() // Key and Value
  .map(
    changes => {
      return changes.map(c => ({
        key: c.payload.key, ...c.payload.val()
      }));
    });
}

I want to display the total Price of all the added products on my Cart 我想在我的购物车上显示所有添加产品的总价格

My Firebase Structure 我的Firebase结构

My Cart 我的车

You can calculate the total after you get the data in orderList$: 您可以在orderList $中获取数据后计算总数:

total = 0;

this.orderList$.map(value =>{
   this.total = this.total + value.price;
}).subscribe();

And in HTML outside the ion-list : ion-list之外的HTML ion-list

<ion-item>
  <ion-label text-wrap>
     <p class="pr" style="font-weight: bold; color: black">Total :</p><p class="pr" style="color: red"> {{total}}</p>
  </ion-label>
</ion-item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM