Is it possible to simplify a service that provides a single gettable / settable variable?
I have a service like this:
var app = angular.module('someModule');
app.factory('token', function() {
var token = null;
return {
get: function() {
return token;
},
set: function(tokenIn) {
token = tokenIn;
}
};
});
I would prefer something like this:
app.variable('token', null);
After 5 years of POJOs, I have a small but intense hatred of getters / setters. Is there a better way to do this using angular? Providing .constant and provided.value didn't seem to be in line with the bill. I couldn't find a plugin to do this either: is there some kind of vocabulary for this concept that I am not aware of?
+1
source to share
1 answer
You can just use the object directly with something like:
myApp.factory('token', function(){
return { Token : '' };
});
Then you can inject a token
factory into your controller and either get or set the token like this:
myApp.controller("Test", function ($scope, token) {
// set:
token.Token = 'abc';
// or get
var local = token.Token;
});
This is about as close to "POJO" as you can get. Hope it helps!
+4
source to share