I'm building a scoring system and am trying to calculate a score based on values in a separate collection. I've been using this example: Calculating sum of repeated elements in AngularJS ng-repeat , but haven't been able to get it to work as I believe they're using one collection and not two.
Here is my template using the suggestions from the question above:
<div>{{parent.id}}</div>
<div>{{ getTotal() }}</div>
<div ng-repeat="child in children | orderBy: '-postedTime' | filter: {parentId: parent.id}">
<div>{{child.score}}</div>
</div>
The js:
$scope.getTotal = function(){
var total = 0;
for(var i = 0; i < $scope.children.length; i++){
var score = $scope.child.score[i];
total += (child.score);
}
return total;
}
Is there a way to sum all the scores from the filtered children and display on the parent?
You can take the filter response
using as syntax in your children filter and use it in a sum filter which will display the sum of the scores of all children for a parent.
P.S : i did not have the exact JSON for parents and children so i have removed postedTime in children but you can use it in your actual code.