4
I’m having some problems automatically updating a code in Angular.js within my site. The integration is Node.js with HTML, integrating with Angular.js. But I always have to give F5 on the page for it to update the variable "Temperature". I would like to integrate the automatic update into the system, when receiving the POST command on Node.js
UPDATING index.html
<html lang="pt-br" ng-app="angular">
<div ng-controller="myController">{{data}}</div>
<script>
var app = angular.module('angular',[]);
app.controller('myController', function($scope, $http, $interval) {
$scope.data = [];
getData();
function getData () {
var request = $http.get('/Temperatura');
request.then(function(data) {
$scope.data = data + " ºC";
},function(data){
console.log('Error: ' + data);
});
request.finally(function () {
$timeout(5000, getData);
});
// O finally é executado sempre que a requisição terminar de executar, independente se o retorno for sucesso ou erro
});
</script>
Node.js
app.post('/EnviaTemperatura', function(req, res){
temperatura = req.body.temp;
res.send('Temperatura: ' + temperatura);
});
app.get('/Temperatura', function(req, res){
res.send(temperatura);
});
I researched to integrate INTERVAL, an Angular module in this code, but I could not.
How can I turn my project into "Real Time"?
You can use the Angular $timeout to make a request every x seconds/minutes. Or you can use Websockets
– DiegoAugusto
Isn’t timeout just to wait a while and then perform? @Diegoaugusto
– Jéf Bueno
@jbueno Truth, I confused rs. kkk The two are a bit alike
– DiegoAugusto
Luiz, good afternoon. I recommend reading: "Solved" in question title does not seem like forum thing?
– Wallace Maxters