In my Angular controller there are two functions.Normally it should able to access $scope value from other function since it's in the same controller.But i can't access it.I have added two console.log() in both function.First function retrieves the scope value because i can see it prints the value in console.But in the second function, it showing an undefined variable.Hope someone will help me to solve the this issue.
Following is my code,
First function
//Get user id
$scope.getUserId = function() {
UserService.getCurrentUser().then(function(response) {
if (response.query_status == "success") {
$scope.userid = response.data.id;
console.log('User id is: '+ $scope.userid);
}
else {
$scope.userid = null;
}
}, function(error) {
$scope.userid = null;
console.log("Error : Failed to load user data...");
console.log(error);
});
}
Second function
//Access user id
$scope.accessUserId = function() {
console.log('User id from other function is:'+ $scope.userid);
}
I could able to solve this.I needed to use Angular $q.defer() method.There was some delay for processing the response for the other function.