I'm trying to save a variable in my application containing the username, I have a login page (a directive) where I get the username. I tried:
Provider: it save my variable but I can only change before the app load and I need to change the variable after inside a controller.
.provider('username',function(){
var username = 'Default';
return {
setName: function(value) {
username = value;
},
$get: function() {
return {
name: username
}
}
}
Services: it works ok, but if I refresh the page I lose the variable.
.service('username', function () {
var username;
this.setName = function (value) {
username = value;
}
this.name = function () {
return username;
}
})
Can some tell me what is the best approach for that? I'm struggling for hours to come up with something..