I have a function that makes a request to the server and it brings me data in JSON
and I save in the variable numero
any number:
var numero
$scope.Cola = function() {
Ticket.Colaservices({
id_usuario: LoginData.getData().id_usuario,
token: LoginData.getData().token,
id_sucursal: LoginData.getData().id_sucursal,
id_moduloatencion: LoginData.getData().moduloatencion_id
}, function (response) {
$scope.DateCola = response.data;
var rut = JSON.parse(JSON.stringify(response.data.rut.split('.').join('')));
numero = response.data.numero;
$scope.rutsinpunto = rut;
});
};
$scope.Cola();
This occurs when you enter the page. Later I have a button llamar
that communicates with the server and a token
and as response returns a new number, to get it I have to update the function above, the problem is that the return is asynchronous, that is to say that in Any time you can reach me.
To obtain it I do the following:
$scope.llamar = function() {
//mando ciertos datos
Llamado.Llamar({
id_usuario: LoginData.getData().id_usuario,
token: LoginData.getData().token,
id_sucursal: LoginData.getData().id_sucursal,
id_moduloatencion: LoginData.getData().moduloatencion_id
}, function (response) {
// y en la respuesta creo un interval y ejecuto $scope.Cola() y muestro en consola la variable numero para saber cuando cambie, ¡cosa que sí hace!
var resultado = $interval(function () {
$scope.Cola();
console.log(numero);
}, 1000);
// acá creo un watch para detectar el cambio ¡pero no funciona! no reconoce cuando es distinto el numero.
$scope.$watch(numero, function (newValue, oldValue) {
if (newValue === oldValue) {
return;
}
$interval.cancel(resultado);
alert("el nuevo valor es " + newValue + " y el antiguo es: " + oldValue);
});
});
};
How can I solve it? the idea is that when the change is detected as an example, show me the alert
.