最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to use session in angularjs - Stack Overflow

programmeradmin6浏览0评论

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 = "../welepage/weletable";
                    }
                    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 wele page

how to store and use sessions 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 = "../welepage/weletable";
                    }
                    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 wele page

how to store and use sessions in angularjs?

Share Improve this question asked Nov 9, 2016 at 5:59 user6656728user6656728 2
  • i think, the best way to use localStorage module! Or Otherwise you can go for docs.angularjs/api/ngCookies/service/$cookieStore – Manish Singh Commented Nov 9, 2016 at 6:02
  • stackoverflow./a/10629617/4689622 – Nimesh Gami Commented Nov 9, 2016 at 6:10
Add a ment  | 

2 Answers 2

Reset to default 1

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./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis./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>

 angular.module('cookieStoreExample', ['ngCookies'])
  .controller('ExampleController', ['$cookieStore', function($cookieStore) {
     // Put cookie
  $cookieStore.put('myFavorite','oatmeal');
  // Get cookie
  var favoriteCookie = $cookieStore.get('myFavorite');
  // Removing a cookie
  $cookieStore.remove('myFavorite');
}]);

look on

发布评论

评论列表(0)

  1. 暂无评论