Reload an AngularJS table from time to time

1

My AngularJS page displays a table with data coming from a JSON. What I want is for the table to be updated from time to time because the data is updated second by second and I display only the last 10.

<tr ng-repeat="linha in linhas">
  <td ><abbr title="{{linha.descricao}}"> <a href=xxx.php?id_chamado={{linha.chamado}}>{{linha.chamado}}</a> </abbr>  </td>  
  <td >{{linha.data}}</td>
  <td >{{linha.nome}}</td>
  <td >{{linha.acao}}</td>
  <td >{{linha.cliente}}</td>   
  <td ><span class="label label-success">{{linha.sistema}}</span></td>
</tr>

Is there any simple way to reach this goal?

My controller, everything works perfectly in the load of the page. I just do not know how to invoke this function again inside the controller.

// JavaScript Document
var app = angular.module('sadApp', []);
app.controller('sadCtrl', function ($scope, $http) {
    $http.get('http://192.168.0.14/a/historicochamadologlidosJson.php').
    success(function(data,  status, headers, config) {
        $scope.linhas = data;
    })
});

I've tried this code here:

angular.element(document.getElementById('MainWrap')).scope().$apply();

where MainWrap is the id of the div:

<div ng-controller="sadCtrl" id="MainWrap"> 
    
asked by anonymous 02.07.2015 / 21:13

1 answer

2

You can try the code below. What I did was wrap in a "private" function the section that fetches the data, and call this function both at startup and at intervals determined by a timer:

// JavaScript Document
var app = angular.module('sadApp', []);
app.controller('sadCtrl', function ($scope, $http) {

    // Função que atualiza os dados
    // Somente disponível aqui dentro, não é método público
    function atualizaDados() {
        $http.get('http://192.168.0.14/a/historicochamadologlidosJson.php').
        success(function(data,  status, headers, config) {
            $scope.linhas = data;
        });
    }

    // Executa a função na inicalização
    atualizaDados();

    // Executa novamente a cada 60 segundos (60.000ms)
    // Altere o intervalo para o que achar mais adequado.
    setInterval(atualizaDados, 60000);
});
    
02.07.2015 / 23:23