Any insights on how this system operates would be greatly valued. My service stores session-specific variables, like table sorting preferences, and other related data.
The code appears to be functioning correctly. However, my query pertains to retrieving these values without directly returning the variable. Do I need to implement a Getter function for this purpose? Are these variables somehow made public through the setter method, or is there an error in my approach? Could it be because of my utilization of this, or is it simply a lack of understanding concerning JavaScript scoping? :)
Here's the snippet of the Service code:
angular.module('myApp').service('sessionService', function () {
this.searchView = 2; // <-- edited in, forgot this
return {
setSearchView: setSearchView
};
function setSearchView (searchView) {
this.searchView = searchView;
}
});
Then, we move on to the controller.
angular.module('myApp').controller('SearchCtrl', function (sessionService) {
console.log(sessionService.searchView); //undefined
sessionService.setSearchView(1);
console.log(sessionService.searchView); // 1
});
After modifying the searchView from the controller, it can then be effortlessly accessed as illustrated above.
I welcome any help in comprehending this matter.
EDIT: I neglected to mention that this.searchView was actually present initially, yet the same outcome prevails in the console.log within the controller.