1
I have an array of orders, plus one of them contains an additional value. I made the sum with $watchCollection, plus it is returning Nan result. How do you add these values, when the object does not have the additional value.
.controller('MyCtrl', function($scope) {
$scope.seuPedido = [
{nome: 'produto 1', valor: 10.00, quantidade: 3},
{nome: 'produto 2', valor: 20.00, quantidade: 1},
{nome: 'produto 3', valor: 30.50, quantidade: 1, adicional: 10}
];
$scope.$watchCollection('seuPedido',function() {
$scope.total = 0;
angular.forEach($scope.seuPedido, function(value, key) {
$scope.total += value.quantidade * (value.valor + value.adicional);
console.log($scope.total)
})
});
});
Follows the codepen: http://codepen.io/alexandre_developer/pen/bpwzzB?editors=0011
Would there be no rule for additional value? How should the calculation be if it does not exist?
– Marconi
Wouldn’t it be cool if you put a Ternario before making the sum, something like
var valorAdicional = value.adicional == undefined? 0 : value.adicional;$scope.total += value.quantidade * (value.valor + valorAdicional);
– Marconi