0

I try to load data from http.get like this:

$http.get("/riddles").success(function(data){
    $scope.riddles = data;
    $scope.riddlesCount = $scope.riddles.length;
});

And if I call console.log($scope.riddles) outside this function, $scope.riddles is undefined, but if I call console.log($scope.riddles) inside $http.get, $scope.riddles is that what I want. I have also a function:

$scope.test = function() {
    console.log($scope.riddles);
}

And when I call it using ng-click, when app load, its work correctly.

What am I doing wrong?

Sachin PATIL
  • 745
  • 1
  • 9
  • 16
K.Dzien
  • 187
  • 12
  • What is the problem? – Explosion Pills Mar 10 '17 at 18:04
  • because that is how asynchronous calls work. It is like ordering a pizza. You want to eat the pizza as soon as you place the order. You can't you need to wait. – epascarello Mar 10 '17 at 18:05
  • "outside this function" == "before the function has been called" – Quentin Mar 10 '17 at 18:05
  • Well, it's simply because `success` (even though now it should be `then`) only executes when the call is done. If you call your `console.log` outside, the http call is not done yet and `$scope.riddles` is not initialized. – ssougnez Mar 10 '17 at 18:05

0 Answers0