The following is the Main Controller implementation:
angular.module("HomePageApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
self.posts = BaseService.fetch['YPosts']();
self.logoutUser = function() {
console.log(self.posts);
BaseService.logout();
};
}]);
This is my BaseService code snippet (some return objects are omitted for simplicity):
angular.module("BaseApp", [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}])
.config(['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);
}])
.factory("BaseService", ["$http", "$window", function($http, $window) {
var cerrorMessages = [];
var posts = {};
return {
fetch: {
XPosts: function() {
$http.get('/postsX/')
.then(function(response) {
posts = response.data;
}, function(response) {
posts = {};
cerrorMessages = BaseService.accessErrors(response.data);
});
return posts;
},
YPosts: function() {
$http.get('/postsY')
.then(function(response) {
posts = response.data;
console.log(posts);
return posts;
}, function(response) {
console.log('here');
posts = {};
cerrorMessages = BaseService.accessErrors(response.data);
});
}
}
};
}]);
When inspecting the posts
in the BaseService
, it contains objects. However, upon clicking the logout
button (which also inspects posts
), it reports that it is undefined. Any insights into why this might be happening?