I am trying to load a JSON file into a factory and retrieve its value.
Below is the code snippet:
angular.module('getGuilds', [])
.factory('getGuilds', getGuilds);
getGuilds.$inject = ['$http'];
function getGuilds($http){
var obj = {content:null};
$http.get('guild/guilds.json').success(function(data) {
obj.content = data;
});
return obj;
}
The issue I am facing is that it only returns an object with a null value, indicating that the $http.get function is not updating the value of obj.content.
To troubleshoot this problem, I conducted a small test:
$http.get('guild/guilds.json').success(function(data) {
obj.content = data;
});
console.log(obj)
return obj;
}
Instead of the JSON array, the test outputted an object like this: {content:null}.
Subsequently, I moved the console.log statement inside the $http.get request.
$http.get('guild/guilds.json').success(function(data) {
obj.content = data;
console.log(obj)
});
Surprisingly, it displayed the contents of the JSON file in the logs. Can someone please assist me with resolving this issue?