I would really appreciate some feed back on how this works? I have a service in which I store some session specific variables such as table sorting choices and more.
The code seems to work fine. However, my question is - how is it possible for me to retrieve these values without returning the actual variable?
Shouldn't I have to use some kind of Getter function? Are they made public some way with the setter method, or am I doing something completely wrong here? Is using this some kind of culprit? Or simply my lack of understanding of javascript scoping? :)
Service code:
angular.module('myApp').service('sessionService', function () {
this.searchView = 2; // <-- edited in, forgot this
return {
setSearchView: setSearchView
};
function setSearchView (searchView) {
this.searchView = searchView;
}
});
And then we have the controller.
angular.module('myApp').controller('SearchCtrl', function (sessionService) {
console.log(sessionService.searchView); //undefined
sessionService.setSearchView(1);
console.log(sessionService.searchView); // 1
});
Once I've set for example the searchView from the controller, it is possible to just access it as below.
Anything that can help me understand this would be appreciated.
EDIT: I forgot to add that this.searchView was actually there form the start, same result in console.log in the controller though.