3

Just finished a Codeacademy tutorial using angular to loop through some JSON data from a URL and display the data.

Would love to know how to implement it so the data would be updated if the JSON data was changing periodically!

I know I need to maybe refresh the http get request,but after that I'm not sure.

If someone could point me in the right direction I would really appreciate it!

Thanks!

services/forecast.js

app.factory('forecast', ['$http', function($http) { 
return $http.get('http://json-time.appspot.com/time.json') 
        .success(function(data) { 
          return data; 
        }) 
        .error(function(err) { 
          return err; 
        }); 
}]);

controllers/MinorController.js

app.controller('MinorController', ['$scope', 'forecast', function($scope, forecast) { 

 var setTimeOut = setInterval(function () {
          forecast.success(function(data) { 
          $scope.fiveDay = data; 
        });
       }, 5000);

       $scope.$on('$routeChangeStart', function (scope, next, current) {
           if (next.$$route.controller != "MinorController") {
               clearInterval(setTimeOut); // clear interval here
           }
       });    
}]);

test.html (view)

 <!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Angular -->
<script src="js/shared/angular.min.js"></script>


<!-- Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css">

<!-- Bootstrap JS -->
<script src="js/shared/bootstrap.min.js"></script>

</head>

<body ng-app="myApp">

        <div class="container" ng-controller="MinorController">
            <div class="row">
                <h1>Time JSON Example</h1>
            <h2>{{ fiveDay.tz }} </h2>
            <h2> {{ fiveDay.hour }} </h2>
            <h2> {{ fiveDay.datetime }} </h2>
            <h2> {{fiveDay.second }} </h2>
            </div>



        </div>

<!-- Modules -->
<script src="js/app.js"></script>

<!-- Controllers -->
<script src="js/controllers/MainController.js"></script>
<script src="js/controllers/MinorController.js"></script>

<!-- Services -->
<script src="js/services/forecast.js"></script>

</body>
</html>
11
  • You can do ti by using interval Commented Apr 13, 2015 at 11:00
  • The problem I have is the data in the JSON object isn't actually changing so I can't really test it properly. The code you provided below still shows the data all the same! Commented Apr 13, 2015 at 11:15
  • hmm, I think your back-end data may by does not changed on run time, So you still seeing the same(old) data. Once your back-end(database) data is changed, then you can see the changed data on run time . Commented Apr 13, 2015 at 11:17
  • Thanks a mil. I'm going to try it out on some data that is changing and let you know! Commented Apr 13, 2015 at 11:19
  • Ok so changed the back-end data to use this data json-time.appspot.com/time.json, and added the code you provided. The data is being displayed, but isn't updating as the data updates! Commented Apr 13, 2015 at 12:01

2 Answers 2

2

You can do it by using setInterval like so.

app.controller('MinorController', ['$scope', 'forecast', function($scope, forecast) {
    var setTimeOut = setInterval(function () {
        forecast.success(function(data) {
            $scope.fiveDay = data;
        });
    }, 5000);

    $scope.$on('$routeChangeStart', function (scope, next, current) {
        if (next.$$route.controller != "MinorController") {
            clearInterval(setTimeOut); // clear interval here
        }
    });
}]);
  • Now your service will call every 5 seconds and rebind your object.

  • If you go to another screen, then the routeChangeStart event will call for the interval to be cleared.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it on several ways:

  • You can make http request in loop every few seconds

    setInterval(function(){ alert("Hello"); }, 3000);

  • If you develop server data source you can setup real time connection, using i.e. SignalR or Socket

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.