angular - 调用 Angular 服务导致无休止的循环
问题描述
我正在从 CartComponent.html 模板调用 getSubtotal() 方法,如下所示:
<div class="col-lg-4 bg-white text-right">
<p class="font-bold">
{{cartService.getSubtotal() | currency:'USD':'symbol-narrow'}}
</p>
</div>
这是 CartService 的 getSubtotal 方法,它现在只记录 cartItems 对象:
public getSubtotal() {
var subtotal:number = 0;
console.log('getSubtotal() ', this.cartItems)
// for(let item of this.cartItems) {
// subtotal += this.getItemPrice(item);
// }
return subtotal;
}
显然 getSubtotal 方法被无休止地调用。它循环打印:
getSubtotal() (13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
解决方案
推荐阅读
- sonarqube - 声纳本地问题 SB4062:无法从程序集中加载“IsTestFileByName”任务
- reactjs - react-hook-form Controller发生错误时如何聚焦?
- python - python中基于逗号的列文本拆分
- amazon-web-services - docker build build-args 在 buildspec.yml 文件中包含空格不起作用
- javascript - 可排序,防止重复项目,不工作
- java - 我可以在 Stripe Express Onboarding 中预填手机号码字段吗?
- python - 热门创建绘图以可视化结构的模式
- redirect - 重定向回原点时被 CORS 策略阻止:标头的值 ... 不等于提供的原点
- python - 在电力预测中使用 Keras 的 lstm 和 ANN 的比较
- javascript - 无法将数据从反应发送到快递服务器