title: How to set a value to a global variable inside a function in javascript/angular?
body: <p>Well, I need to use a variable in different angular controllers or any function, which I can do, but in this particular case I'm getting the value of one of those variables  from a service or a function, how you want to call it, this is the code </p>
  
  <pre><code>//global variable userdata
  userdata = {};
  
  ng.tcts.service('$user', function($http, $q, $window, $boots){
  return{
  
      login: function(data){
          var deferred = $q.defer();
  
          $http({
              method:'POST',
              url:ng.api+'/log/in',
              data:{
                      data:data
                  },
              headers:{'Content-type':'application/x-www-form-urlencoded'}        
          }).success(function(response, status, headers, config){
              if(response.success){
                  console.log(response);
                  //userdata assignation
                  userdata = response.data;  
                  deferred.resolve([response.respnum, response.success, response.report, response.data]);
              }
              else{
                  deferred.resolve([response.respnum, response.success, response.report, response.data]);
              }
          }).error(function(response, status, headers, config){
              deferred.reject([response.respnum, response.success, response.report]);
          });
          return deferred.promise;        
      }
  
  );
  </code></pre>
  
  <p>As  you can see I'm asigining userdata the value of response.data inside the success function , when I use it in the function environment it works , the assignation works fine, but when I try to use it outside of it, the object is empty, I hope you can help me with this, thx</p>