Call method every so often

0

My app.js

$scope.leerProductos = function() {

  InitService.leerProductos().then(
         function(d) {
           $scope.productos = d;
            }
        );
};

My html:

<div ng-init="leerProductos()">
  <div ng-repeat="pro in productos">
        {{pro.name}} y {{pro.precio}}
  </div>
</div>

How can I call this method every 5 seconds?

    
asked by sirdaiz 10.04.2017 в 09:13
source

2 answers

0

The solutions like this:

$scope.leerProductos = function() {


var promise = $interval(function()    { 
    InitService.leerProductos().then(
      function(d) {
        $scope.productos = d;
    }
                );
        }, 
 5000);

$scope.$on('$destroy', function ()   { 
   $interval.cancel(promise); 
});
    
answered by 10.04.2017 / 10:44
source
0

This will help you, in the link there is an example of what you need, Link of refencia

$scope.leerProductos = function() {
    InitService.leerProductos().then(
        function(d) {
            $scope.productos = d;
        }
    );

    var promise = $interval(function() 
    { 
        //Aqui es cuando llama tu función
        $scope.leerProductos();
    }, 
    5000);

    $scope.$on('$destroy', function () 
    { 
        $interval.cancel(promise); 
    });
};
    
answered by 10.04.2017 в 09:25