In my factory, how to extract(read) data from the .json file before returning to the calling function from a directive?
Here is the definition of my factory
(function () {
"use strict";
angular
.module("common.services")
.factory("langService", ["$resource", langService])
function langService($resource) {
var langData = $resource('services/langInfo.json').query();
function getInfo(language){
/*look for a specific code from langData and return*/
}
return {
getInfo: getInfo
}
}
}());
getInfo() is the function that is supposed to lookup for the appropriate info from langData variable and return it.
here is the json data from langInfo.json (not complete yet):
[{
"marathi": { },
"hindi": { },
"konkani": { },
"sanskrit": { },
"gujarati": { }
}]
I am new to AngularJs hence need help. I suppose langData is not resolved as I tried printing it with console.log, hence I am not able to access it. This is what i got with
console.log(langData)
By the way what I am trying to do is possible? How?
EDIT:
according to Stian Sandve's answer, I passed a call back function at the call to langService as:
langService.getInfo('langCode', function(data) {
console.log(data);
});
and changed the definition of getInfo to
function getInfo(language, callback){
langData.query(function(data) {
var filteredData = filter(data); //assume filter() is defined
callback(filteredData);
});
}
So I am getting the data in callback function. But I need to set it to some variable that is declared before the call to langService.getInfo. So something like this:
var langInfo
langService.getInfo('langCode', function(data) {
console.log(data);
//set data to langInfo
});