4
Example: I have any ng-repeat, which repeats each one a number. How do I store a variable that is equal to the sum of this number of each repeat?
Code Angularjs:
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 there are, even if it is inserted in the future more merchandise, as mercadoria2, mercadoria3, mecadoria4, then the total would have to change according to the number of inserted goods.
I have already researched a lot, but in everything I see, it seems that it would be necessary to insert, to each new merchandise, a new installment in the sum. Ex: {{listademercadoria[0]['total']}}+{{listademercadoria[1]['total']}}+{{listademercadoria[2]['total']}}
etc....