I have the following script:
<script>
var xmlhttp = new XMLHttpRequest();
var myApp = angular.module("myApp", []);
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
response = this.responseText;
var myObj = JSON.parse(response);
console.log(myObj);
}
};
xmlhttp.open("GET", "functions.php?action=getuserdata&id=" + document.getElementById('userID').innerHTML, true);
xmlhttp.send();
console.log(myObj);
myApp.controller("myCtrl", function ($scope) {
$scope.firstName = myObj[0].firstname;
$scope.lastName = myObj[0].lastname;
$scope.email = myObj[0].email;
$scope.cell = myObj[0].cell;
$scope.domainname = myObj[0].domainname;
$scope.tfamethod = myObj[0].tfamethod;
$scope.said = myObj[0].said;
});
</script>
Now my understanding is that the 'var' keyword, assigns a variable globally which is accessible from anywhere within the window. I set the var within the onreadystatechange function, and I'm able to see it when doing the first console.log, but I get it as 'undefined' when I log it again outside the function. Is my understanding of var incorrect? If so, how do I fix the code to be able to access myObj from outside the function?