I'm using two $watches
on my controller that are supposed to take an eye at 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 the Charts.js and angular-charts plugins) read data from them. I've put the charts in custom directives that receive the data from an attribute, and they're working properly.
The problem is, that I want to create another chart who reads another object that is equal to these, but calculates it's data 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;
}
These are the watches (I tried both watching the object
and the object.data
variable):
$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 doesn't work. You see all the console.logs
? I only see the "SCOPE" console.log
, once (not even twice for ganancias
). When I change the data in some inputs that are bound to these two objects, everything works (the charts get real-time updated) but the beneficios
chart doesn't, aswell these watches just don't work.
Am I doing something wrong at these two watches?