0

im using AngularJS for value calculator on the client side. I want to update the main value of this calculator every 5 minutes using setInterval().

My AngularJS code is:

 $http({method: 'GET', url: '../assets/sources.json'}).success(function(data)
   {
      $scope.values = data; // response data

      $scope.getSourceValue = function(){

         if ($scope.source == "test") {

            return $scope.values.test["last"]

         } else if ($scope.source == "test1"){

            return $scope.values.test1["last"]

         } else if ($scope.source == "test2"){

            return $scope.values["test2"]["last"]

         } else {

            return -1
         };

      } // getSource
    } 

On client side:

<strong><h1> {{getSourceValue()|currency}}</strong></h1>

Can someone point me in the right direction?

Thanks in advance.

UPDATE:

{
    "timestamp": "Sun Sep 14 2014, 01:40:03",
    "bitstamp": {
        "display_URL": "http://www.bitstamp.net",
        "display_name": "Bitstamp",
        "currency": "BTC",
        "last": 477.6
    },
    "btc-e": {
        "display_URL": "http://www.btc-e.com",
        "display_name": "BTC-e",
        "currency": "BTC",
        "last": 471.5
    },
    "bitcoinaverage": {
        "display_URL": "http://api.bitcoinaverage.com",
        "display_name": "BitcoinAverage",
        "currency": "BTC",
        "last": 479.23
    },
    "geeklab": {
        "display_URL": "http://ws.geeklab.com.ar",
        "display_name": "Geeklab",
        "currency": "ARS",
        "blue": 14.35
    }
}
6
  • Can you provide the content of your json file ? Commented Oct 26, 2014 at 2:51
  • are you using $scope.$apply() in the setInterval to update the bindings? using setInterval takes you out of the angular digest. AngularJS and Apply Commented Oct 26, 2014 at 3:02
  • No, I really don't know how to do it, I was trying with setTimeInterval for JS but didn't work with Angular (duh). Commented Oct 26, 2014 at 3:03
  • possible duplicate of AngularJS value won't change with setTimeout Commented Oct 26, 2014 at 3:06
  • 1
    Use $timeout and not setTimeout Commented Oct 26, 2014 at 3:12

1 Answer 1

0

I'll post the solution for someone else. My approach:

angular.module('test', [])

   .controller('test', function($scope, $http, $filter, $interval) {



      var stop;

      if(!angular.isDefined(stop)) {
         stop = $interval(function(){


               $http({method: 'GET', url: '../assets/sources.json'}).success(function(data)
               {
                  $scope.values = data; // response data

                  $scope.svalue = 0;

                  $scope.getSourceValue = function(){

                     if ($scope.source == "bitcoinaverage.com") {

                        $scope.svalue = $scope.values.bitcoinaverage["last"]

                     } else if ($scope.source == "bitstamp.net"){

                        $scope.svalue = $scope.values.bitstamp["last"]

                     } else if ($scope.source == "btc-e.com"){

                        $scope.svalue = $scope.values["btc-e"]["last"]

                     } else {

                        $scope.svalue = -1
                     };

                  } // getSource


            });//  /success()


         }, 40000);
      }


}); /*Controller*/
Sign up to request clarification or add additional context in comments.

1 Comment

You're redefining the $scope.getSourceValue in every ajax call. Move it to the outside scope and prefer using the 'this' aproach, put in $scope only if the function is shared with the child controllers. Other tip, the controller is the resposible for controlling the model<->view interation. Put the logic and data resources gathering into a service. :)

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.