javascript sessionStorage 数据不会保留在带有 ngStorage 的 AngularJS 应用程序中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24086891/
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-10-28 02:10:42  来源:igfitidea点击:

sessionStorage data not persist in AngularJS app with ngStorage

javascriptangularjssession-storageangular-services

提问by danny-v

All,

全部,

I'm simply trying to store a user object in sessionStorage in an AngularJS app. If I step through this in either the Chrome or FF debugger, the sessionStorage never gets set. Here is my angular service code:

我只是想在 AngularJS 应用程序的 sessionStorage 中存储一个用户对象。如果我在 Chrome 或 FF 调试器中逐步执行此操作,则 sessionStorage 永远不会设置。这是我的角度服务代码:

// Authentication/authorization Module
stonewall.authModule = angular.module("authModule", [
    // Module dependencies
    "ngStorage"
    ]);

// Authentication/authorization service tracks the current user
stonewall.authModule.service('authService', function ($localStorage, $sessionStorage) {

    // Initialize current user
    var currentUser = {};
    restoreSession();

    // Declare storage type-this may change if user selects
    // "Keep me signed in"
    var storageType = {};

    // Return the current user object
    this.getCurrentUser = function () {
        return currentUser;
    };
    // Returns whether there is a currently authorized user
    this.userAuth = function() {
        return currentUser.sid != "";
    };
    // Logout function, initializes the user object
    this.logout = function() {
        currentUser = {
            sid: "",
            status: 0,
            pswLastSet: 0,
            id: "",
            sigUID: "",
            sig: ""
        };
        //persistSession();
    };
    // Login
    this.login = function(user, subj) {
        if (user == null) return;
        currentUser = {
            sid: user.Principal.SId,
            status: user.Principal.ControlStatus,
            pswLastSet: new Date(user.Principal.PasswordLastSet),
            id: user.Identity.Id.DN,
            sigUID: user.Identity.Certificates[0].UID,
            sig: stonewall.hash(user.Principal.SId + subj.pswd),
        };
        persistSession();
    };
    // Persist to session storage
    function persistSession() {
        $sessionStorage.currentUser = currentUser;
    };
    // Restore session
    function restoreSession() {
        currentUser = $sessionStorage.currentUser;
        if (currentUser == null) {
            // Initialize to empty user
            currentUser = {
                sid: "",
                status: 0,
                pswLastSet: 0,
                id: "",
                sigUID: "",
                sig: ""
            };
        }
    };
});

And, here is a screencap that shows my FF debugging session. You can see that after persistSession is called that $sessionStorage has my user.

而且,这是一个屏幕截图,显示了我的 FF 调试会话。您可以看到在调用persistSession 之后, $sessionStorage 拥有我的用户。

Debugger

调试器

But, if I switch over to the DOM inspector, sessionStorage has no items in it...

但是,如果我切换到 DOM 检查器, sessionStorage 中没有任何项目......

DOM

DOM

Any help is, as always, appreciated.

一如既往,任何帮助都值得赞赏。

采纳答案by martin.code

Are you sure you are using angular's sessionStorage in the right way? Session storage is a property of the $window object in angular, so I don't know if you have made your own service wrapper or something like that? Anyway, here is a codepen that shows another approach that I use myself, using $window.sessionStorage instead: http://codepen.io/chrisenytc/pen/gyGcx

您确定以正确的方式使用 angular 的 sessionStorage 吗?会话存储是 angular 中 $window 对象的一个​​属性,所以我不知道您是否制作了自己的服务包装器或类似的东西?无论如何,这是一个代码笔,它显示了我自己使用的另一种方法,使用 $window.sessionStorage 代替:http://codepen.io/chrisenytc/pen/gyGcx