First here's my code below. My main problem is I can't return the result data outside my invokeConfigFile function after invoked or after ajax call.
var invokeConfigFile = function() {
return $http.get('json/config.json');
};
var data = {}
var getDataFromInvokedFile = function() {
invokeConfigFile().then(function(result) {
var current_env = result.data.current_environment,
list_env = result.data.environments;
angular.forEach(list_env, function(value, key) {
// retrive the config on the current environment
if(key === current_env) {
data = value;
return false; //let's stop if the condition are met
}
});
console.log(data); // return data GOOD
return data;
}, function(result) {
if(result.status == 404) {
alert("Server config.js file not found.");
return false;
}
});
console.log(data); // return data EMPTY
};
var my_urls = getDataFromInvokedFile();
What I'm trying to accomplish here is after I get the result data from my ajax call which is invokeConfigFile(), I will then store this to an object.
Something like this
return {
my_obj : getDataFromInvokedFile()
}
console.log(data);that returns empty will be running prior to the call finishing. You should place this inside a callback function to make sure the value is set, or make the Ajax call Synchronous.return { my_obj : getDataFromInvokedFile()}?