Angularjs ng repeat内重复项目最后一次出现后的显示元素(跨度标签)
我想在ng repeat中显示这样的内容:显示一个span标记,表示每个人上次购买后的总购买量。(我不想在每次购买后显示总购买量)。我有一个对象数组,如下所示:Angularjs ng repeat内重复项目最后一次出现后的显示元素(跨度标签),angularjs,Angularjs,我想在ng repeat中显示这样的内容:显示一个span标记,表示每个人上次购买后的总购买量。(我不想在每次购买后显示总购买量)。我有一个对象数组,如下所示: let group=[{name:'Brandon Pack',city:'NY',purchase:25,accepted:true}, {name:'Josh Vilet',city:'Memphis',purchase:30,accepted:true}, {name:'Brandon Pack',city:'
let group=[{name:'Brandon Pack',city:'NY',purchase:25,accepted:true},
{name:'Josh Vilet',city:'Memphis',purchase:30,accepted:true},
{name:'Brandon Pack',city:'NY',purchase:62,accepted:true},
{name:'Patrick Whiteside',city:'NY',purchase:50,accepted:false},
{name:'Josh Vilet',city:'Memphis',purchase:50,accepted:true}]
我可以得到总数,我的问题是,我不想只在该人员的最后一次检查后才显示总数。首先,您可以在控制器中创建一个函数以获得总数:
$scope.getTotal = function () {
var sum = 0;
$scope.group.forEach(function(customer){
sum += customer.purchase;
});
return sum;
}
顺便说一下,您不能声明名称中带有空格的变量,比如“let group”,最好称之为“letGroup”
然后,您只需在Html中创建表:
<table>
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Purchase</th>
<th>Accepted</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="o in group">
<td ng-bind="o.name"></td>
<td ng-bind="o.city"></td>
<td ng-bind="o.purchase"></td>
<td ng-bind="o.accepted ? 'Yes': 'No'"></td>
</tr>
<tr>
<td></td>
<td></td>
<td>Total</td>
<td ng-bind="getTotal()"></td>
</tr>
</tbody>
</table>
名称
城市
购买
认可的
全部的
一个JSFIDLE示例将有助于理解您的问题。