How can I get other http.get response inside in a ng-repeat

69 Views Asked by At

I have this HTML http://pastebin.com/DegTb0iH

has a ng-repeat rulling in rullings within it need to do the vote count of each staff, get these values as follows: http://pastebin.com/EB4KigwS

the problem is there on the lines 17,18 and 19 HTML, totalVotes always returns 0 in JS (or only the last result), if you do the console.log in the success comes the correct result of each type of vote

Can anyone give me a help?

1

There are 1 best solutions below

2
On

You're calling the function three times using a different value for voto_type (fair enough) but all three of these, on the success callback, will store the result in $scope.totalVotes which means they will always display the same and last result.

You need to store each result into a separate $scope variable.

E.g. JS:

if (angular.isUndefined($scope.totalVotes)) {
    $scope.totalVotes = {};
}
if (result.total > 0) {
    $scope.totalVotes[voto_type] = result.total;
} else {
    $scope.totalVotes[voto_type] = 0;
}

and HTML:

<p ng-init="getTotalVotes(rulling.id, 's')">Positivos: {{totalVotes['s']}}</p>
<p ng-init="getTotalVotes(rulling.id, 'n')">Negativos: {{totalVotes['n']}}</p>
<p ng-init="getTotalVotes(rulling.id, 'a')">Abstenções: {{totalVotes['a']}}</p>