Javascript 如何在 AngularJs 中存储和读取会话(值)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34130086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:56:43  来源:igfitidea点击:

How to store and read session (value) in AngularJs?

javascriptangularjssession

提问by Rakesh

I am having trouble in store and read sessioned data with Angularjs. After click on button emp_name should store in sessionand also how can i read stored emp_name from session.

我在存储和读取会话数据时遇到问题Angularjs。单击按钮后 emp_name 应存储在,session以及如何从session.

Sample in plnkr

plnkr 中的示例

// Code goes here

var app = angular.module('app', []);

app.controller('Ctrl', function($scope) {
  $scope.employee = [{
      emp_id: 1,
      emp_name: 'Jes',
      emp_cont:9876543445
    }, {
      emp_id: 2,
      emp_name: 'Sandy',
      emp_cont:3553454345
    }, {
      emp_id: 3,
      emp_name: 'Alex',
      emp_cont:9343434345
    }, {
      emp_id: 4,
      emp_name: 'Nancy',
      emp_cont:9876543445
    }, {
      emp_id: 5,
      emp_name: 'Scott',
      emp_cont:9834564455
    }
  ];
  
          $scope.returnRefId = function (emp) {           
            try {
                  //  test emp
                  alert(emp);
                  // session code here
                  
            }
            catch (e) {
                  alert("some errror");
            }
        };
});
ul li{list-style:none;float:left;padding:10px;border:1px solid #ddd;height:20px;width:100px}
ul{clear:both}
label{color:red}
<!DOCTYPE html>
<html>

<head>
  <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="app" ng-controller="Ctrl">
  <ul ng-repeat="employees in employee | filter:customFilter">
    <li>{{employees.emp_id}} </li>
    <li>{{employees.emp_name}} </li>
    <li>{{employees.emp_cont}} </li>
    <li><button   ng-click="returnRefId(employees.emp_name);" >Add Session</button></li>    
  </ul>
  <br /><br /><br />
  <label id="read_ses">How to store and read emp_name thru only session</label>
</body>

</html>

I have created alert with emp_name on click by using angularjs. I want that on button click emp_name should store in sessionand for sessiontesting purpose read sessioned emp_name data somewhere on page.

我使用angularjs. 我希望按钮点击 emp_name 应该存储在页面上的某个地方session,为了session测试目的读取会话的 emp_name 数据。

Any and all help/advice is sincerely appreciated. Thanks.

真诚地感谢任何和所有帮助/建议。谢谢。

回答by Omkar Khair

Your session storage code could look something like this using Javascript APIs for session storage. You will have to serialize your Javascript object as session storage only supports strings.

您的会话存储代码可能类似于使用 Javascript API 进行会话存储的代码。您必须序列化您的 Javascript 对象,因为会话存储仅支持字符串。

$scope.returnRefId = function (emp) {           
        try {
              //  test emp
              alert(emp);
              // session code here
              sessionStorage.setItem("emp-key", JSON.stringify(emp));
        }
        catch (e) {
              alert("some errror");
        }
 };

Alternatively, you can storage each property of empin a separate storage key. Debugging should be as simple as

或者,您可以将 的每个属性存储emp在单独的存储密钥中。调试应该很简单

sessionStorage.getItem("emp-key")

sessionStorage.getItem("emp-key")

More information on session storage can be found here. https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage

可以在此处找到有关会话存储的更多信息。https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage

Also checkout ngStorage https://github.com/gsklee/ngStorage

还可以查看 ngStorage https://github.com/gsklee/ngStorage

回答by Maurizio Vacca

AngularJS applications are stateless, so there's no really a session. However, you can fake the session behaviour using the local storage. Here's the ngStorage, a AngularJS module you can use to model you session.

AngularJS 应用程序是无状态的,所以没有真正的会话。但是,您可以使用本地存储来伪造会话行为。这是ngStorage,这是一个 AngularJS 模块,可用于为会话建模。

In general, you create a service to access data contained in the local storage and then you inject the service whenever you need to access the session data.

通常,您创建一个服务来访问包含在本地存储中的数据,然后在需要访问会话数据时注入该服务。

Best!

最好的事物!

回答by Micha? Kolenda

HTTP session is stored in server side and AngularJS works on client's side. If you want to access the HTTP session from Angular application, you will need to implement a service on server side ant then call it from Angular using httpService.

HTTP 会话存储在服务器端,AngularJS 工作在客户端。如果您想从 Angular 应用程序访问 HTTP 会话,您需要在服务器端 ant 上实现一个服务,然后使用 httpService 从 Angular 调用它。