我有这个清单,我想创建每个项目的总和'数据部分,并在视图中显示。
//this data
var list=[{
id:1,
data:[
{name:'item 1',qty:100,price:1},
{name:'item 2',qty:150,price:1.5}
]
},{
id:2,
data:[
{name:'item 1',qty:100,price:1},
{name:'item 2',qty:150,price:1.5}
]
}]
//此总和乐趣
function countmoney(l){
count_listmoney=0;
for(i=0;i<l.data.length;i++){
count_listmoney+=l.data[i].price*l.data[i].qty
}
return count_listmoney
}
HTML
<div id="viewtable">
<div ng-repeat="l in list">
<!--Is there an alternative better way?-->
<div>{{countmoney(l)}}</div>
</div>
</div>
有没有比从这个视图中调用函数更好的方法?
答案 0 :(得分:0)
首先处理您的数据,然后在视图中显示:
// ideally in a service
var sums = []
for (var i=0; i < list.length; i++) {
sums.push(countmoney(list[i]))
}
// controller
$scope.sums = sums; // better: $scope.sums = dataService.getSums()
HTML
<div id="viewtable">
<div ng-repeat="sum in sums">
<div>{{sum}}</div>
</div>
</div>
答案 1 :(得分:0)
简单方法
<div id="viewtable">
<div ng-repeat="l in list">
Sum : {{l.price*l.qty}}
</div>
</div>