AngularJS ngcontroller to be reloading data periodically

后端 未结 8 1745
自闭症患者
自闭症患者 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:40

    You can use the setInterval function but you need to encapsulate your logic in a function like this:

    app.controller('MainCtrl', function($scope, $http) {
    
      // Function to get the data
      $scope.getData = function(){
        $http.get('style.css')
          .success(function(data, status, headers, config) {
    
            // Your code here
            console.log('Fetched data!');
          });
      };
    
      // Run function every second
      setInterval($scope.getData, 1000);
    
    });
    

    I've created a working plnkr for your at: http://plnkr.co/edit/vNCbBm45VYGzEQ7cIxjZ?p=preview

    Hope that helps!

    0 讨论(0)
  • 2020-12-14 02:40

    You can try the implementation of promises/deferred using $q provider.

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

    0 讨论(0)
  • 2020-12-14 02:49

    While jvandemo's answer will work, I think it can be improved slightly. By using setInterval, it breaks the dependency injection convention that Angular follows and makes unit testing of the controller difficult.

    Angular doesn't currently support setInterval through its built-in services, but you can use the $timeout service to produce the same functionality. I'd change the controller to this:

    app.controller('MainCtrl', function($scope, $http, $timeout) {
    
      // Function to get the data
      $scope.getData = function(){
        $http.get('style.css')
          .success(function(data, status, headers, config) {
    
          // Your code here
          console.log('Fetched data!');
        });
      };
    
      // Function to replicate setInterval using $timeout service.
      $scope.intervalFunction = function(){
        $timeout(function() {
          $scope.getData();
          $scope.intervalFunction();
        }, 1000)
      };
    
      // Kick off the interval
      $scope.intervalFunction();
    
    });
    
    0 讨论(0)
  • 2020-12-14 02:49

    This answer builds off of drew.walker's answer, but makes it so that changing controllers will not spawn multiple endlessly running refreshes. It also runs getData immediately, instead of delaying it 1 second.

    It depends on you using Angular routes and setting Ctrls in them. If you don't, you'll need another way to determine if this controller is in scope.

    app.controller('MainCtrl', function($scope, $rootScope, $route, $timeout) {
    
        // Function to get the data
        $scope.getData = function() {
            ...
        };
    
        // Get the data immediately, interval function will run 1 second later
        $scope.getData();
    
        // Function to replicate setInterval using $timeout service.
        $scope.intervalFunction = function() {
            var currentCtrl = $route.current.$$route.controller;
            var currentlyRunning = $rootScope.myAppMainCtrlRefreshRunning;
            //do not run if the MainCtrl is not in scope
            //do not run if we've already got another timeout underway (in case someone jumps back and forth between
            //controllers without waiting 1 second between)
            if (currentCtrl === "MainCtrl" && !currentlyRunning) {
                $timeout(function() {
                    $rootScope.myAppMainCtrlRefreshRunning = true;
                    $scope.getData();
                    $scope.intervalFunction();
                    $rootScope.myAppMainCtrlRefreshRunning = false;
                }, 1000);
            };
        };
        // Kick off the interval
        $scope.intervalFunction();
    
    });
    
    0 讨论(0)
  • 2020-12-14 02:56
    Try this it worked fine for me.
    
    $scope.intervalFunction = function(){
         if ($location.$$absUrl === "current url") {
          $timeout(function() {
          $scope.showAllEmployeeTracking();
          $scope.intervalFunction();
          console.log("loading Interval")
        }, 10000)
        }
      };
    
      // Kick off the interval
      $scope.intervalFunction();
    
    0 讨论(0)
  • 2020-12-14 02:57

    I had to change to order to make it work:

    From

    $scope.intervalFunction();
    $rootScope.myAppMainCtrlRefreshRunning = false;
    

    to

    $rootScope.myAppMainCtrlRefreshRunning = false;
    $scope.intervalFunction();
    

    Otherwise the loop only executes once

    0 讨论(0)
提交回复
热议问题