1

I am trying to get values from a JSON file using this function:

function getarray(){
  $http.get('http://localhost/prebuilt/countries.json')
  .success(function(data) {
    $scope.countries = data;
  });
  return data;
} 

I need these return values to be used in another function in the same control block. The problem is that it doesn't return anything.

my controller starts like this:

angular.module('theme.charts-flot', [])
       .controller('FlotChartsController', 
                   ['$scope', 
                    '$timeout', 
                    '$http',
                    '$parse',
                    function ($scope, $timeout, $http, $parse) {

and ia m using a template. this is the controller for the chart that is being updated real time.

glepretre
  • 8,154
  • 5
  • 43
  • 57
Tanimak
  • 134
  • 1
  • 9

1 Answers1

1

In AngularJS you do not have to return the data. It uses data binding, so it is sufficient to assign the data to $scope.countries and its value will be updated. However, you have to parse the data in the HTTP response like this, if you are not using the default transformation of the response:

$scope.countries = JSON.parse(data);

If you want to return something you can always return $scope.countries. The data is out of the local scope of the success function and therefore it will be undefined at the return statement.