drenzzo
10/20/2017 - 5:21 PM

Crear variables de sesion

Crear variables de sesion

var app = angular.module('app');
    
    
app.factory('myFactory', ['$window', function($window){
    var addVariableSession = function(key, newObj) {
      var mydata = newObj;
      if (!_.isEmpty(mydata)) 
        $window.sessionStorage.setItem(key, JSON.stringify(mydata));
      else
        $window.sessionStorage.setItem(key, "");
    };

    var getVariableSession = function(key){
     var mydata = $window.sessionStorage.getItem(key);
      if (!_.isEmpty(mydata)) {
          mydata = JSON.parse(mydata);
      }
      return mydata || [];
    };
    
    var eliminarVariableSession = function(key) {
      $window.sessionStorage.removeItem(key);
    };   
}]);

//how to use
myFactory.addVariableSession('nameVarSession');

var myVarSession = myFactory.getVariableSession('nameVarSession');

myFacory.eliminarVariableSession('myVarSession');