I am writing a web app in Angular, this piece of code is meant to get test XML data as text from the server. I am using Angular's $http service to send a GET request from the server. The variable 'xmlData' is being changed inside the $http block, but outside that block its value is still the default value. Here's the code:
this.getData = function() {
var xmlData = "Default";
var xmlDoc;
// send an http GET request for the XML text data
$http.get('http://localhost:1337/testXML.txt').success(function(data) {
xmlData = data;
if (xmlData == null) {
alert("Data Error Occurred");
}
// here, 'xmlData' contains the correct information.
}).
error(function() {
alert("HTTP Error Occurred");
});
// once the above $http block ends, the xmlData has the default value.
return xmlData;
}
So is this just a silly scope issue? Or am I using Angular's $http service incorrectly? Thanks!