Javascript 范围错误最大调用堆栈超出角度6

Javascript 范围错误最大调用堆栈超出角度6,javascript,angular,Javascript,Angular,我正在做购物车应用程序,当用户从清单中取消选择项目时,我需要从总金额中减去,并且每次用户通过共享服务添加或删除时,我都使用行为主体更新总金额。总金额减去后,我再次相应地更新主题值。但这里我得到了错误堆栈 onItemDeSelect(uncheckedItem) { this.eachItem.itemExtraOptionPrice.prices.forEach(item => { this.shared.updateAmountValue.subscribe(val

我正在做购物车应用程序,当用户从清单中取消选择项目时,我需要从总金额中减去,并且每次用户通过共享服务添加或删除时,我都使用行为主体更新总金额。总金额减去后,我再次相应地更新主题值。但这里我得到了错误堆栈

onItemDeSelect(uncheckedItem) {
    this.eachItem.itemExtraOptionPrice.prices.forEach(item => {
      this.shared.updateAmountValue.subscribe(value => {
        if (item.id === uncheckedItem.id) {
          this.totalAmount = value - item.amount;
          this.shared.updatedAmount(this.totalAmount);
        }
      });
    });
}

updateAmountValue: BehaviorSubject<number> = new BehaviorSubject(0);

  updatedAmount(value) {
    this.updateAmountValue.next(value);
  }
onItemDeSelect(取消选中){
this.eachItem.itemExtraOptionPrice.prices.forEach(item=>{
this.shared.updateAmountValue.subscribe(值=>{
if(item.id==uncheckedItem.id){
this.totalAmount=值-item.amount;
this.shared.updatedAmount(this.totalAmount);
}
});
});
}
updateAmountValue:BehaviorSubject=新的BehaviorSubject(0);
updatedAmount(值){
this.updateAmountValue.next(值);
}

在这里,每次对取消选择的项执行一次MDESELECT()函数,然后更新共享服务中的总金额。我不知道我在哪里出错。

最大调用堆栈超出错误主要发生在函数进行无限递归时。这在代码中不是很明显,你订阅了一些东西,再次更新了值。在您的代码中,您正在执行

this.shared.updatedAmount(this.totalAmount);
这将更新该值并激发一个偶数,与行为主体相同

updateAmountValue: BehaviorSubject<number> = new BehaviorSubject(0);

这不会导致任何递归条件。希望这有帮助。

你能发布updatedAmount()的代码吗?我已经更新了PostTQ。
onItemDeSelect(uncheckedItem) {
    this.eachItem.itemExtraOptionPrice.prices.forEach(item => {
      let value = this.updateAmountValue.getValue();
        if (item.id === uncheckedItem.id) {
          this.totalAmount = value - item.amount;
          this.shared.updatedAmount(this.totalAmount);
        }
    });
}