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