Click here to Skip to main content
16,006,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two functions inside the controller ,But i couldn't print variables outside the function .How can we fix the problem?

JavaScript
var getlockerbalance=function(){

           $http.get("php/selectLOCKERBALANCE.php")
           .success(function(lockerbalance){
           $scope.lockerbalance1=lockerbalance;
            var   lastlockerbalance=$scope.lockerbalance1[0].LOCKERBALANCE;
          console.log(lastlockerbalance);

            })

         }


What I have tried:

<pre lang="Javascript">
var getlockerbalance=function(){
					
		       $http.get("php/selectLOCKERBALANCE.php")
		       .success(function(lockerbalance){
		       $scope.lockerbalance1=lockerbalance;
				var   lastlockerbalance=$scope.lockerbalance1[0].LOCKERBALANCE;
		      console.log(lastlockerbalance);
				
                })

console.log(lastlockerbalance);

The error says that lastlockerbalance in undefined
Posted
Updated 6-Feb-17 3:37am

1 solution

anything within {...} that is declared (var, let, const) is inaccessible outside the {..}.

This is called scope. Read up on it. It is very important!
Everything you wanted to know about JavaScript scope[^]

In order to access it outside the scope, you must either declare it outside, or extend an object that has been declared outside (such as scope for example, but this is generally bad practice).

Declare outside scope:
JavaScript
var   lastlockerbalance = null; //declared in outer scope so in available in outer & inner scope.
var getlockerbalance=function(){
	$http.get("php/selectLOCKERBALANCE.php")
	    .success(function(lockerbalance){
	        $scope.lockerbalance1=lockerbalance;
                //updated in inner scope still updates the outer var
		lastlockerbalance=$scope.lockerbalance1[0].LOCKERBALANCE;
		console.log(lastlockerbalance);
	    })

console.log(lastlockerbalance);


The alt. is to extend an object that is declared outside the scope (pretty much the exact same thing if you understood the scope and dom:
JavaScript
var   item = {}; //declared in outer scope so in available in outer & inner scope.
var getlockerbalance=function(){
	$http.get("php/selectLOCKERBALANCE.php")
	    .success(function(lockerbalance){
	        $scope.lockerbalance1=lockerbalance;
                //simply add a property to the item object
		item.lastlockerbalance=$scope.lockerbalance1[0].LOCKERBALANCE;
		console.log(lastlockerbalance);
	    })

console.log(item.lastlockerbalance);


Please don't make a habit of doing the second version. If you do then you should always declare the objects property first:
var item = {lastlockerbalance:null};
or
var $scope.item = {lastlockerbalance:null};

hope that helps
Andy ^_^
 
Share this answer
 
Comments
Karthik_Mahalingam 6-Feb-17 12:07pm    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900