AngularJS ngcontroller to be reloading data periodically

后端 未结 8 1746
自闭症患者
自闭症患者 2020-12-14 02:17

I have this working implementation af a angularJS app which fetches some links from an URL and paint them. However the links on the URL are being updated constantly, I woul

相关标签:
8条回答
  • 2020-12-14 02:59

    2016


    $interval is made for this:

        $interval(function() {
            // your stuff          
        }, 1000);
    

    Don't forget to inject $interval in your controller

    app.controller('ExampleController', ['$scope', '$interval',function($scope, $interval) {
          $interval(function() {
                // your stuff          
          , 1000); 
    }]);
    

    https://docs.angularjs.org/api/ng/service/$interval

    0 讨论(0)
  • 2020-12-14 03:04

    A better solution is to use Angular $interval and $destroy providers Example:

    var stop=$interval(function () {
                function_call();
            }, 12000)
    
            $scope.stopInterval = function () {
                if (angular.isDefined(stop)) {
                    $interval.cancel(stop);
                    stop = undefined;
                }
            };
    
            $scope.$on('$destroy', function () {
                // Make sure that the interval is destroyed too
                $scope.stopInterval();
            });
    
    0 讨论(0)
提交回复
热议问题