简体   繁体   English

$ sessionStorage以及如何在AngularJs中查看和使用$ sessionStorage

[英]$sessionStorage and how to view and work with $sessionStorage in AngularJs

I have been doing some research regarding cookies, sessionStorage, localStorage, but come up with very little insight into $sessionStorage. 我一直在做一些有关cookie,sessionStorage,localStorage的研究,但是对$ sessionStorage的了解很少。

Where can I view $sessionStorage in Developer Tools (if one can)? 在哪里可以在开发人员工具中查看$ sessionStorage(如果可以)? Or is there and alternative, and even better way? 还是有替代方法,甚至更好的方法?

I am using AngularJs' $sessionStorage in my app to assist with user experience, keeping track of filters and dates etc. 我在我的应用程序中使用AngularJs的$ sessionStorage来辅助用户体验,跟踪过滤器和日期等。

I have looked for these parameters under Chrome's Application>>Local Storage, Session Storage Cookies and all other options, but cannot find my APP defines $sessionStorage values. 我在Chrome的“应用程序>>本地存储”,“会话存储Cookie”和所有其他选项下查找了这些参数,但找不到我的APP定义了$ sessionStorage值。 They work though! 他们虽然工作! :D :D

Update: Here is an example of the usage: 更新:这是用法示例:

CookiesAlertController.$inject = ['$scope','$http','$sce','$sessionStorage','GetDataService'];

function showAlert() {
    if (!angular.isDefined($sessionStorage.cookiesAlertNotificationCount)) {
        $sessionStorage.cookiesAlertNotificationCount = 0;
    }
    if (angular.isDefined($sessionStorage.cookiesAlertNotification)) {
        if ($sessionStorage.cookiesAlertNotificationCount === false && $sessionStorage.cookiesAlertNotificationCount > 4) {
            $scope.cookiesAlertNotification = false;
        }
        else {
            $scope.cookiesAlertNotification = true;
        }
    }
    else {
        $scope.cookiesAlertNotification = true;
    }
}

$scope.closeNotification = function () {
    $scope.cookiesAlertNotification = false;
    $sessionStorage.cookiesAlertNotification = false;
    $sessionStorage.cookiesAlertNotificationCount = $sessionStorage.cookiesAlertNotificationCount + 1;
};

$sessionStorage is an AngularJs service found at: https://github.com/gsklee/ngStorage $sessionStorage是AngularJs服务,位于: https : //github.com/gsklee/ngStorage

It allows you to read and write to $localStorage and $sessionStorage , making use of these stored parameters during the lifetime of your session. 它允许您读写$localStorage$sessionStorage ,并在会话有效期内使用这些存储的参数。

You will not be able to view these $sessionStorage or $localStorage items via the dev tools, but you can view the values by assigning it to a variable in your code. 您将无法通过开发工具查看这些$sessionStorage$localStorage项目,但可以通过将其分配给代码中的variable来查看这些值。

Example: 例:

var whatIsInMyStorage = $localStorage; // or $sessionStorage; 

You can also clear these storage items by calling: 您还可以通过调用以下命令清除这些存储项目:

$sessionStorage.$reset();
$localStorage.$reset();

I hope this helps someone else. 我希望这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM