I am a newbie at angular and not too strong in javascript to begin with. I'm trying to make an app using ionic framework. And im trying to get a list from a json file. I've successfully made it work with the raw json in a variable. But i'm now trying to use $http.get()
to retrieve it from a remote file in my project folder.
.service('staffServices', ['$q', '$http', staffService]);
function staffService($q, $http) {
var staffs = {};
var promise = $http.get("/js/jsons/settings.json")
.success(function(response) {
staffs = response.staffSettings;
console.log(staffs); //HAS WHAT I NEED
});
console.log(staffs); //EMPTY OBJECT
return {
loadAllSettings: function () {
return $q.when(staffs);
},
query: function (params) {
return filterFilter(staffs, params);
},
get: function (params) {
return this.query(params)[0];
}
}
};
For some reason i can't access the result outside of the .success()
function. I'm not sure if this is due to my ignorance in javascript or my newbie status in angular. Please help me out
This is my controller below. self.settings
never gets populated and always returns an empty object
.controller("staffCtrl", function (staffServices) {
var self = this;
self.settings = [];
staffServices.loadAllSettings()
.then(function (settings) {
self.settings = [].concat(settings);
console.log(settings);
});
});