吸气剂未返回正确的值

时间:2020-07-25 14:23:58

标签: angular

我正在尝试汇总所有小计并返回总计,但是我得到了狂野的值,并且我经常遇到错误:表达式在检查后已更改。

当我使用

import { ChangeDetectionStrategy } from '@angular/core';

@Component({
    changeDetection: ChangeDetectionStrategy.OnPush
}) 

它抑制了表达式已更改的错误,但我仍然获得总计的野生值。

这是在我的组件中:

totals : number[ ] = [ ];

get grandTotal() {

    let i;
    let sub_total = 0;
    let grand_total = 0;
if  (this.isSubmitted == true){
    sub_total = this.product_price * this.quantity;

    if (typeof this.product_price  === "undefined") {
        return 0;
    } else {
        this.totals.push(sub_total);
        for (i = 0; i < this.totals.length; i++) {
            grand_total += this.totals[i];
this.isSubmitted = false;
        }
        return grand_total;
    }
}
}

这是在我的HTML中;

<div>Grand Total {{ grandTotal }}</div>

2 个答案:

答案 0 :(得分:0)

好的-我了解您想要这样的东西。 Order

几周前,我完成了一个项目,您想实现的目标是什么。 我将尝试解释它在此站点上的工作原理,将产品添加到列表中的操作,并计算整个订单的总和。

客户选择产品,选择数量并添加到订单中。 product

如果订单是新订单,则会创建一个新的订单文档,并向该文档中添加产品的新集合,客户将选择其他产品并将其添加到同一订单中

由于在将产品添加到列表时将其保存在Firebase文档中,因此它还会添加一些其他参数。这样。

enter image description here

,并且如第一个图像一样收集此数据以进行检出。 我得到了这个产品清单。在OrderService中

getProdListInOrder(user, orderid) {
    return this.db.collection('users').doc(user).collection('ordersout')
      .doc(orderid).collection('products').snapshotChanges().pipe(
        map(action => action.map(as => {
          const id = as.payload.doc.id;
          const data = as.payload.doc.data() as OrderProdData;
          return { id, ...data };
        }))
      );
  }

在使用此方法的checkOut组件中,我向客户显示有关订单的所有信息; 在这种方法中

getProdInOrder() {
    this.osrv.getProdListFromOrder(this.USERID, this.PEDIID).subscribe(prolist => {
      this.listaPR = prolist;
      this.LISTAPROD.emit(prolist);
      this.listaprice = [];
      //extract only totalprice to the list
      this.listaPR.forEach(tp => {
        this.listaprice.push(tp.totalprice);
      });
      this.TotalTicket = this.listaprice.reduce((ac, cur) => ac + cur, 0);
      this.TOTALPRICE.emit(this.TotalTicket);
    });
  }

在这种方法中,我仅提取总价并添加到“标价”中,并具有减少所有订单总和的功能。 您也可以计算价格总和,而无需提取总价。

this.TotalTicket = this.listaPR.map(t => t.totalprice).reduce((ac, cur) => ac + cur, 0);

如果您还有其他问题,请随时提出。

答案 1 :(得分:0)

我能够通过在onSubmit函数中将isSubmitted设置为true,并在获取方法的结尾将其设置为false来解决此问题。我已经编辑了帖子以显示此内容。

相关问题