如何在angularjs中使用会话

用户名

我创造,我想存储的用户ID和角色在Web应用程序session通过Angularjs

这是我的Angularjs文件

$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";
                    }
                }
            })

我需要存储$scope.pm$scope.log在我的会话中,并想在我的欢迎页面上使用相同的内容

如何在angularjs中存储和使用会话?

吉加尔7521

您可以为此使用会话存储或本地存储,这是会话存储的示例。

会话存储或本地存储在堆栈溢出时将无法正常工作,因此请参考给定的链接。

关联

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章