angular - 从网格列 Ionic 4 获取总值
问题描述
有人可以帮我解决这个问题。我想得到最后一列值的总值。下面是我的 HTML 代码。
<div *ngFor="let order of orders |filter:searchText">
<ion-row>
<ion-col>
<ion-text>
{{order.OrderNo}}
</ion-text>
</ion-col>
<ion-col>
<ion-text>
{{order.CustomerName}}
</ion-text>
</ion-col>
<ion-col>
<ion-text>
{{order.PaymentType}}
</ion-text>
</ion-col>
<ion-col>
<ion-text>
{{order.TotalPrice | currency:''}}
</ion-text>
</ion-col>
</ion-row>
</div>
<h6>Total value: </h6>
解决方案
您可以在模板中使用 javascript,例如:
<h6>Total value: {{ orders.reduce((acc, nxt) => acc + nxt) | currency:'' }}</h6>
但是,在模板中包含逻辑通常是不好的做法。在您的组件中创建一个函数并从您的模板中调用它:example.component.ts
// replace any with actual type
sum(orders: any[]): number {
return orders.reduce((acc, nxt) => acc + nxt);
}
example.component.html
<h6>Total value: {{ sum(orders) | currency:'' }}</h6>
推荐阅读
- hibernate - SpringBoot 2.0.4 中同一实体的多个表示
- nlp - python中的依赖解析
- javascript - 如何在 JavaScript 中添加位
- validation - 01/01/0001 当通过出生日期来控制
- javascript - 将getJSON的结果分成两个部分,并使用每个部分显示不同的DIV
- python - 在 Chrome 中仅关闭 1 个选项卡
- android - 如何在不同的活动中将文本设置为 TextView
- java - 如何在 macOS 上将路径参数传递给 java
- javascript - JavaScript:运算符优先级
- amazon-cloudwatch - 使用字段点键过滤 CloudWatch 中的 JSON 日志事件