Example: I have a ng-repeat any, which repeats each a number. How do I store a variable that is equal to the sum of this number of each repeat?
Angular CodeJS:
ngular.module('meumodulo', [])
.controller('mercadoria', function($rootScope, $http) {
var ctrl = this;
$rootScope.listademercadoria = [];
$rootScope.mercadoria0 = {
id: 'id1',
setor: 'setor1',
foto: 'foto1',
descr: 'descr1',
de: de1,
por: por1,
mercadoria: '0',
quantidade: 1,
total: ''
}
$rootScope.listademercadoria.push($rootScope.mercadoria0);
$rootScope.mercadoria1 = {
id: 'id2',
setor: 'setor2',
foto: 'foto2',
descr: 'descr2',
de: 'de2',
por: 'por2',
mercadoria: '1',
quantidade: 1,
total: ''
}
$rootScope.listademercadoria.push($rootScope.mercadoria1);
$rootScope.showPanel = true;
$rootScope.listademercadoria.push($rootScope.mercadoria1);
$rootScope.remover = function(b) {
{
$rootScope.showPanel = !$rootScope.showPanel;
}
}
});
HTML code:
...
<div class="body" ng-controller="mercadoria">
<span>Total dos produtos: {{listademercadoria[0]['total']}}</span>
</div>
...
These codes only show the value of the "total" variable within $ rootScope.mercadoria0.
It is necessary to show the sum of the variables "total" of all the repeats that exist, even if more goods are inserted in the future, such as commodity2, commodity3, mechanic4, then the total would have to change according to the number of goods inserted .
I have researched a lot, but in everything I see, it seems that it would be necessary to insert, for each new commodity, a new portion in the sum. Ex: {{listademercadoria[0]['total']}}+{{listademercadoria[1]['total']}}+{{listademercadoria[2]['total']}}
etc ...