Mumbai Wadala Mumbai Wadala - 1 year ago 73
AngularJS Question

how to use session in angularjs

I am creating a web app in which i want to store userid and role in

session
through
Angularjs


this is my Angularjs file

$http.get('/login.asmx/loginuser', {
params: {
log: $scope.log,
pm: $scope.pm,
password: $scope.password
}
})
.then(function (response) {
{
$scope.suc = response.data;
console.log(response.data);
if (response.data == 'success') {
console.log('success');
$window.location.href = "../welcomepage/welcometable";
}
else
{
console.log('nosuccess');
$window.location.href = "../Login/Index";
}
}
})


i need to store
$scope.pm
and
$scope.log
in my session and want to use the same on my welcome page

how to store and use sessions in angularjs?

Answer Source

You can use session storage or local storage for that, here is the example of session storage.

session storage or local storage will not work here in stack overflow so refer given link.

Link

window.addCart = function($scope, $http, $window, $document){
  
    var getValue = function(){
        return $window.sessionStorage.length;
    }
      
    var getData = function(){
      var json = [];
      $.each($window.sessionStorage, function(i, v){
        json.push(angular.fromJson(v));
      });
      return json;
    }
      
    $scope.images = getData();
    $scope.count = getValue();
  
    $scope.addItem = function(id){
        var image = document.getElementById('img'+id);
        json = {
          id: id,
          img: image.src
        }
        $window.sessionStorage.setItem(id, JSON.stringify(json));
        $scope.count = getValue();
        $scope.images = getData();
    }
    
    $scope.removeItem = function(id){
      $window.sessionStorage.removeItem(id);
      $document.
      $scope.count = getValue();
      $scope.images = getData();
      alert('Removed with Success!');
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div ng-app="">
    <div ng-controller="addCart">
      <p>{{ count }}</p>
      <div>
        <img id="img16" src="http://placehold.it/351x350"/>
      <a href="javascript:void(0)" ng-click="addItem('16')">Add to Cart</a>
      </div>
      <div>
        <img id="img5" src="http://placehold.it/352x350"/>
      <a href="javascript:void(0)" ng-click="addItem('5')">Add to Cart</a>
      </div>
      <div>
        <img id="img32" src="http://placehold.it/353x350"/>
      <a href="javascript:void(0)" ng-click="addItem('32')">Add to Cart</a>
      </div>
      <div>
        <img id="img43" src="http://placehold.it/354x350"/>
      <a href="javascript:void(0)" ng-click="addItem('43')">Add to Cart</a>
      </div>
      <hr />
      <table>
        <thead>
          <td>Image</td>
          <td>Options</td>
        </thead>
        <tbody>
          <tr ng-repeat="data in images" id>
            <td><img ng-src="{{ data.img }}"/></td>
            <td><a href="javascript:void(0)" ng-click="removeItem(data.id)">Remove Item {{ data.id }}</a>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
</div>