Looking to create an object of data in an angular factory i can return to a controller but having some issues:
app.factory('simpleFactory', function ($http) {
var mykey = 'akjhas3892kh8adsak9';
var apiResource = [{
type: 'type1',
abbr: 'abc'
}, {
type: 'type2',
abbr: 'efg'
}, {
type: 'type3',
abbr: 'hij'
}];
var data = {};
var setDataKeyValues = function (key, value) {
data[key] = value
};
for (var i = 0; i < apiResource.length; i++) {
var key = apiResource[i].type;
$http.get('http://api.com/v1/path/' + key + '/' + apiResource[i].abbr + '/anotherpath?apikey=' + mykey).success(function (info) {
setDataKeyValues(key, info);
});
}
return data;
});
The Angular http request is returning a JSON object, but when the data object is returned, it is empty. What is the correct why to add these key/values to this object?