我有一个数组,它将在html中列出*ngFor
,这个数组将有一些值我需要计算html中列出的值,我不应该使用.ts文件,而我需要总数要在另一行显示的值
<ion-grid>
<ion-row *ngFor="let item of dailyDays">
<ion-col>{{item.price}}</ion-col>
</ion-row>
</ion-grid>
<ion-row> total should be here </ion-row>
dailyDays = [];
this.dailyDays.push({ day: element,
month: this.currentDate.getMonth(),
year: this.currentDate.getFullYear(),
price: data.price,
brand: data.selectedBrand,
howManyDay: data.selectedDay,
check: "true",
quantity: data.selectedQuantity
});
这个价格在页面加载时会有所不同,所以我应该只使用.html文件来获取总价值
final(){
console.log("this will have an array of objects", this.dailyDays);
var array = this.dailyDays;
var total= 0;
for (var i = 0; i < array.length; i++) {
var element = array[i];
console.log(element.price);
// how to add each price and get a total
total += element.price;
console.log(total);
}
}
我得到的问题是
[
答案 0 :(得分:0)
在几次讨论和混淆中我得到了答案,因为在.html中进行计算是我使用的最差方法.ts文件
final(){
console.log("this will have an array of objects", this.dailyDays);
var array = this.dailyDays;
for (var i = 0; i < array.length; i++) {
var element = array[i];
console.log(element.price);
// how to add each price and get a total
this.total += parseFloat(element.price);
}
console.log(this.total);
}
答案 1 :(得分:-1)
只需使用Lodash即可。它就像这样:
<ion-grid>
<ion-row *ngFor="let item of dailyDays">
<ion-col>{{item.price}}</ion-col>
</ion-row>
</ion-grid>
<ion-row>{{_.sumBy(dailiDays, 'price')}}</ion-row>
&#13;