1

i will get data from function, in case this my code..

 var urlServer = 'http://localhost:2205/inventoryApp/server/'; 
  $scope.getReport = function() {
    $http.get( urlServer + 'transaksi.php?action=getreport').success(function(data) {
      $scope.reports = data;
      // console.log($scope.reports.tanggal);
      // if i run this console (inside function) the data is showing in console log
    });        
  };

  $scope.getReport();
  console.log($scope.reports);
 //this console log get undefined

and i get undefined in my console log..

thanks before :)

3
  • So you should check if your http request just fails using .error(). (Beware, both, .success and .error are deprecated) Commented Sep 29, 2015 at 9:52
  • my http request is success, i show data in my view(template) no error and show the data Commented Sep 29, 2015 at 9:54
  • You have to write the console.log(data) in side your callback Commented Sep 29, 2015 at 9:59

2 Answers 2

1

Your code runs synchronously, your request runs asynchronous, so this should work:

var urlServer = 'http://localhost:2205/inventoryApp/server/'; 
  $scope.getReport = function() {
    $http.get( urlServer + 'transaksi.php?action=getreport').success(function(data) {
      $scope.reports = data;
      // console.log($scope.reports.tanggal);
      // if i run this console (inside function) the data is showing in console log
console.log($scope.reports);
    });        
  };

  $scope.getReport();

window.setTimeout(function()
{
    console.log('data should be available now', $scope.reports);
}, 5000);

You just have to wait until your request is finished before you can display its response. :-)

For example, after a few seconds, your data should be available. To make this clean, use promises as you can see here: https://docs.angularjs.org/api/ng/service/$http

Sign up to request clarification or add additional context in comments.

3 Comments

if console.log inside function absoluty work... but if outside get unifed.. so i ask this question
Its not undefined in the outside. Its just undefined when you try to output it. I will update my answer.
i see.. the data need interval to already set. thanks very much thomas :)
1

console is executed before executing get service. try to use .then() function and console in it.

Comments

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.