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
    }
}
mdv
  • 915
  • 3
  • 14
  • 26

1 Answers1

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*/
mdv
  • 915
  • 3
  • 14
  • 26
  • 1
    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 modelview interation. Put the logic and data resources gathering into a service. :) – Rubens Pinheiro Nov 03 '14 at 17:32