I use two $watches on my controller that should keep track of these two objects:
$scope.gastos = { name: "Gastos mensuales", data: [0,0,0,0,0,0,0,0,0,0,0,0], labels: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"] }; $scope.ganancias = { name: "Ganancias mensuales", data: [0,0,0,0,0,0,0,0,0,0,0,0], labels: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"] };
Two charts (from Charts.js and angular -charts plugins) read data from them. I put diagrams in user directives that get data from an attribute and they work correctly.
The problem is that I want to create another graph that reads another object that is equal to them, but calculates it in this method:
function calcularBeneficios(){ var data = []; for(var i=0;i<12;i++){ data[i] = $scope.ganancias.data[i] - $scope.gastos.data[i]; } console.log("FUNCTION DATA: "+data); return data; }
This is a clock (I tried both to look at the variable object and object.data ):
$scope.$watch("gastos", function(){ $scope.beneficios.data = calcularBeneficios(); console.log("SCOPE: "+$scope.beneficios.data); }); $scope.$watch("ganancias", function(){ $scope.beneficios.data = calcularBeneficios(); console.log("SCOPE: "+$scope.beneficios.data); });
This does not work. Do you see all console.logs ? I see only "SCOPE" console.log once (not even twice for ganancias ). When I change the data on some inputs attached to these two objects, everything works (diagrams are updated in real time), but the beneficios diagram does not work, since this clock simply does not work.
Am I doing something wrong in these two hours?