app.controller('tableController', function ($scope, $filter, ngTableParams,$http){
$scope.users = [];
$http.get("getjsondata").success(function (response) {
$scope.users = response; //ajax request to fetch data into $scope.data
});
console.log($scope.users); // I am not getting the updated value as
// I need to pass this value in another function
});

- 9,929
- 5
- 36
- 47

- 21
- 6
-
First check if you are getting any data from JSON which you are getting in response and check browser console if you are getting any error. You can also debug your Angular code to check what is the value of response – user3045179 Apr 26 '16 at 08:17
-
1Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) You can't `console.log` there because `response` is only available _after_ the asynchronous request completes. – Sergiu Paraschiv Apr 26 '16 at 08:18
4 Answers
the success function is a callback after the call is complete if you want to see the value you must call any other functions or pass any values inside the callback
like
$scope.users = [];
$http.get("getjsondata").success(function (response) {
$scope.users = response;
console.log($scope.users);
});

- 291
- 1
- 8
The console.log
statement gets executed right after you issue the Http Request.
You need to log it/interact with it in your success callback.
$http.get("getjsondata").success(function (response) {
$scope.users = response;
console.log($scope.users);
});
Also, try not to use $scope
in your controllers. See John Papa's style guide for Angular.js

- 4,059
- 2
- 21
- 26
Your log message is written outside of the success promise and there is probably being executed before your assignment. Try the following:
$scope.users = [];
$http.get("getjsondata").success(function (response) {
$scope.users = response;
console.log($scope.users);
});
Don't forget promises are asynchronous and as such will mean they get executed later than whatever is coming after the console.log statement.

- 2,818
- 23
- 36
The reason is that console.log($scope.users);
will be called before $http.get().success()
gets executed;
$http.get()
returns a promise.
you can debug this like that:
$http.get("getjsondata").success(function (response) {
console.log('i am after the promise return')
$scope.users = response; //ajax request to fetch data into $scope.data
});
console.log('i get executed first');
console.log($scope.users);
-
Thanks for your way to make this problem understood in a simple way. now i understood the actual processing of statement which will executed first and why it was not actually printing the value due to asynchrouns call. – manish Apr 26 '16 at 09:18