Saggy10
Saggy10

Reputation: 3

javascript variable use outside function

$scope.pagination_data = function(page_data){
   count = page_data.data.count;
};

console.log("outside count is",count);// this not working

var page = function(){
 userService.paginate()
  .then(function(user){
    $scope.pagination_data(user);
    console.log("count is",count);//this is works
  });
};

in that code i m using $http service by using that i m getting data aftr that i want a count then i m getting count also but now i want this count variable access in controller but this is not accessible to me. what should i do..?

Upvotes: 0

Views: 217

Answers (3)

slebetman
slebetman

Reputation: 113896

Actually, both works but you're calling the first one at the wrong time.

The .then() indicates that paginate() returns a promise. Which should be a hint that it is asynchronous.

To prove this, delay calling your first console.log by using setTimeout:

$scope.pagination_data = function(page_data){
   count = page_data.data.count;
};

setTimeout(function(){
    console.log("outside count is",count);// this should work
}, 5000); // wait 5 seconds before calling the code above
// If 5 seconds is not long enough increase it to 10 or something

var page = function(){
 userService.paginate()
  .then(function(user){
    $scope.pagination_data(user);
    console.log("count is",count);//this is works
  });
};

What is asynchronous code? Asynchronous simply means that the code will be called later. What paginate().then() does is not call the function(user) function immediately but rather remembers that it is supposed to call function(user) (which then calls your pagination_data function to set the value of count) at a later time. It then continues on running other stuff.

When there's no other code to run, the event loop gets processed and whatever userService.paginate() needs to do asynchronously gets processed.

When whatever userService.paginate() is waiting for finally returns (this may be a network request, user click etc) the function(user) function finally gets called. This in turn calls $scope.pagination_data() which is what assigns the result to the global count variable.

What does that setTimeout() do? Well, it does the same thing I describe above. Instead of calling console.log() immediately it remembers to call it later. Then when 5 seconds (5000 milliseconds) expires, and there is no other javascript code running (this is important because it means javascript can run the event loop), the console.log() finally gets called.

Upvotes: 1

wonderbell
wonderbell

Reputation: 1126

The fist statement only defines the function, it doesn't call it. If you have data to pass to that function call it before logging the count.

   // this only defines the function, doesn't call it
    $scope.pagination_data = function(page_data){
       count = page_data.data.count;
    };

    console.log("outside count is",count);// count is still unset

    var page = function(){
     userService.paginate()
      .then(function(user){
        $scope.pagination_data(user); // this is the call, count will be set
        console.log("count is",count);//this will work
      });
    };

Upvotes: 0

Safari
Safari

Reputation: 3382

Just define the variable outside.

var count = 0;
$scope.pagination_data = function(page_data){
   count = page_data.data.count;
};

console.log("outside count is",count);// this not working

var page = function(){
 userService.paginate()
  .then(function(user){
    $scope.pagination_data(user);
    console.log("count is",count);//this is works
  });
};

Upvotes: 0

Related Questions