I am new to Angular and not very comfortable with JavaScript. I am attempting to create an app using the Ionic framework. I have successfully retrieved a list from a JSON file stored in a variable, but now I am trying to use $http.get()
to fetch it from a remote file in my project directory.
.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); //RECEIVES THE DATA
});
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];
}
}
};
I am having trouble accessing the result outside of the .success()
function. I'm not sure if this is because of my lack of knowledge in JavaScript or my beginner level in Angular. Any assistance would be appreciated.
This is my controller below. self.settings
always remains empty and does not populate
.controller("staffCtrl", function (staffServices) {
var self = this;
self.settings = [];
staffServices.loadAllSettings()
.then(function (settings) {
self.settings = [].concat(settings);
console.log(settings);
});
});