AngularJS - Watch array and update object -


i'm trying figure out how watch array , update object, this.

var vm = this; //controller vm.order = {     totalvalue: 25,     products: [{id:0, val: 10, qtd: 1}, {id:1, val: 15, qtd: 1}] }; 

if push new product order object, how update totalvalue watching collection ? (totalvalue = val * qtd)

it necessary because "qtd" field bind input field, can change value of "products" @ time.

update

$scope.$watchcollection(             function() { return self.order.products; },              function(products) {                 self.order.totalvalue = products.reduce(function (p, n) {                     console.log(n.qtd);                     console.log(n.val);                     return p + n.qtd * n.val;                 }, 0);             }); 

the code worked, when push new "product" order. have watch every order.products.product.qtd , change order.totalvalue every time user change qtd of product

try way:

$scope.$watch('order.products', function (products) {     scope.order.totalvalue = products.reduce(function (p, n) {         return p + n.qtd * n.val;     }, 0); }, true); 

this deep watch array detect qtd , val changes inside array items.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -