-1

i am using simple get method

var self= this;
self.items = [];
var fetchGraphs = function() {
        return $http.get('/api/graph').then(
                    function(response) {
                            self.items = response.data;
                            alert(self.items);
                    }, function(errResponse) {
                            console.error('Error while fetching notes');
                    });
         };` 

to get the following JSON data.

{
 "Msg": "Running",
 "excMsg": null,
 "array1": {
        "key1": 32,
        "key2": 10,
        "key3": 24
    },

 "array2": {
        "key4": 42,
        "key5": 20,
        "key5": 22
    }}

Now I have to draw graph for array1 and array2. for that i have to declare the values from get method inside script itself.but its giving me error for

"can not read key1 property."

Can anyone please tell me how to declare array variables that I get from JSON in the another script file

BrownHash
  • 1
  • 2

2 Answers2

1

You cannot "declare" variables direct. You must use method or subscribe to event. The easy solution scope.emit and etc. See here: Working with $scope.$emit and $scope.$on If you want to use fetchGraphs for example (your method).

fetchGraphs().then(function(result) {
   // do something with result array 
});

Edit:

If you post more code people here can help you effective.

Community
  • 1
  • 1
Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
1

You have to use then method from fetchGraph or else return data from success method of $http.

My recommendation, you can keep watch on the self.items. so whenever it change you can re-draw your graph.

$scope.$watch(
    "self.items",
     function () {
          //re-draw your graph
          $scope.$apply(); // not needed always, just a precaution
      }
);
Avinash Jain
  • 7,200
  • 2
  • 26
  • 40