我无法理解为什么下一个代码无效。
DateTime
在该代码之前,我有一个如下所示的数组保留:
DateTime?
结果我得到了类似的东西:
<div ng-repeat="r in reservations" ng-init="new = r.some_code">
<div ng-if="old == new">
<p ng-init="sum = sum + r.cost">{{r.name}} - {{r.cost}}</p>
</div>
<div ng-if="old != new">
<p ng-init="old = new;">Total: {{sum}}</p>
</div>
</div>
我尝试了很多次,但结果不一致。我想要的是显示每个预订,如果“reservations = [{name: client 1, some_code = 15, cost: 1000},
{name: client 1, some_code = 15, cost: 1000},
{name: client 2, some_code = 15, cost: 1000},
{name: client 3, some_code = 16, cost: 2000},
{name: client 4, some_code = 16, cost: 3000},
{name: client 5, some_code = 17, cost: 3000}]
$scope.old = reservations[0].some_code;
$scope.sum = 0;
”发生变化,则显示总数。
我该怎么做?
注意:数组按15 - client 1 - 1000
15 - client 2 - 1000
15 - client 3 - 1000
Total: 0
Total: 0
排序。
答案 0 :(得分:0)
您可以通过some_code呈现预订,然后有选择地显示详细信息。皮肤猫的方法:JSFiddle
答案 1 :(得分:0)
在你的阵列中你有两个客户端1,不确定它是否相关。
我认为将此求和逻辑移动到控制器可能更好。您可以使用some_code作为键创建哈希,将total作为值。
它看起来像这样
$scope.reservations = [{name: client 1, some_code = 15, cost: 1000},
{name: client 1, some_code = 15, cost: 1000},
{name: client 2, some_code = 15, cost: 1000},
{name: client 3, some_code = 16, cost: 2000},
{name: client 4, some_code = 16, cost: 3000},
{name: client 5, some_code = 17, cost: 3000}];
$scope.totals = {};
for (var i = 0; i < $scope.reservations.length; i++) {
if (totals[$scope.reservations[i].some_code]) {
totals[$scope.reservations[i].some_code] += $scope.reservations[i].cost;
} else {
totals[$scope.reservations[i].some_code] = $scope.reservations[i].cost;
}
}
&#13;
<div ng-repeat="r in reservations" ng-init="new = r.some_code">
<div>
<p> {{r.name}} - {{r.cost}} </p>
</div>
<div>
<p> Total: {{totals[r.some_code]}}
</div>
</div>
&#13;