【问题标题】:sessionStorage data not persist in AngularJS app with ngStoragesessionStorage 数据不会通过 ngStorage 保存在 AngularJS 应用程序中
【发布时间】:2014-09-04 09:42:10
【问题描述】:

全部,

我只是想将用户对象存储在 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: ""
            };
        }
    };
});

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

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

一如既往地感谢任何帮助。

【问题讨论】:

    标签: javascript angularjs session-storage angular-services


    【解决方案1】:

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

    【讨论】:

    • 感谢壮举,您建议的更改奏效了。我试图使用this library 并遵循他们的 plunkr 示例。我是 html/js 开发的新手,所以我想我在某个地方用了一些东西。
    • 不客气。事实上,我没有看到你在使用这个模块,但如果它现在可以工作,就不需要使用这个模块。提供的代码未表明您是否正确导入了模块。如果您需要有关此特定问题的帮助,您应该上传所有代码,以便我们进行检查。但同样,如果它现在有效,为什么还要麻烦:)
    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2018-03-05
    • 2017-12-19
    相关资源
    最近更新 更多