简体   繁体   English

从html访问$ window.sessionStorage

[英]Access $window.sessionStorage from html

In my case I am working with angularjs and I want to access a saved value in user to show or not some html tags. 在我的情况下,我正在使用angularjs,我想在用户中访问一个保存的值,以显示或不显示某些HTML标签。

First it save in some controller the user variable, it is a part of log in action. 首先,它在一些控制器中保存用户变量,它是登录操作的一部分。

$window.sessionStorage.setItem('user', AuthenticationService.getUser());

Then, I want to show some html tags depending on the role of that user, with something like the following: 然后,我想根据该用户的角色显示一些html标签,具体如下:

<li><a id="id" ng-if="user.role=='admin'" href> console </a></li>

I had something similar to this using $rootScope , but when I refresh the page I lose the value. 我使用$ rootScope有类似的东西,但是当我刷新页面时,我失去了价值。 That is why I need to use window.sessionStorage 这就是我需要使用window.sessionStorage的原因

Is there any way to access in html a saved value in sessionStorage? 有没有办法在html中访问sessionStorage中保存的值? or this is wrong and I need another approach? 或者这是错的,我需要另一种方法?

You can do it via a service/factory 您可以通过服务/工厂完成

angular.module('myApp').factory("PrincipalService", [
    "$window",
    function($window){

        var user;

        return {
            getUser : getUser,
            setUser: setUser
        };

        function getUser(){
            return user || angular.fromJson($window.sessionStorage.getItem('user'));
        }

        function setUser(userInfo){
            user = userInfo;
            $window.sessionStorage.setItem('user', angular.toJson(userInfo));
        }

    }
]);

edit 编辑

The way I could use it to share through every views via rootScope : 我可以使用它通过rootScope分享每个视图的方式:

angular.module("myApp").run(["$rootScope", "PrincipalService",
    function($rootScope, PrincipalService){
        $rootScope.getUser = PrincipalService.getUser;
        $rootScope.setUser = PrincipalService.setUser;
    }
]);

And then, in your views you can call : 然后,在您的观点中,您可以致电:

<span>{{$root.getUser()}}</span>

$window is just a wrapper around window, so this approach will work, but it's more preferable to use some other means, such as cookies than to pollute $window. $ window只是窗口的包装器,所以这种方法可行,但更好的是使用其他方法,如cookie而不是污染$ window。

You should use a controller function or even better, a global user service to access the user. 您应该使用控制器功能,甚至更好的全局用户服务来访问用户。

There's a pretty detailed article on how to do this here: http://www.nesterovsky-bros.com/weblog/2015/01/26/windowsessionStorageInAngularJS.aspx 这里有一篇关于如何做到这一点的非常详细的文章: http//www.nesterovsky-bros.com/weblog/2015/01/26/windowsessionStorageInAngularJS.aspx

If you have multiple user properties, use toJson and fromJson to store the object in session storage such as: 如果您有多个用户属性,请使用toJson和fromJson将对象存储在会话存储中,例如:

var user = { name:'mike', role: 'admin'}
$window.sessionStorage.setItem('user',angular.toJson(user));

and: 和:

var user = angular.fromJson($window.sessionStorage.getItem('user'))

If you need to access the user from a number of different places, a service like @Deblaton recommended is the way to go, but assuming you have multiple properties on your user, add the angular.toJson and angular.fromJson lines like I have above. 如果您需要从多个不同的地方访问用户,建议使用像@Deblaton这样的服务,但假设您的用户有多个属性,请像上面一样添加angular.toJson和angular.fromJson行。 。

The way you are accessing the user in your html requires that it's present on the current scope so if you use the service approach in your controller you would add: 您在html中访问用户的方式要求它出现在当前范围内,因此如果您在控制器中使用服务方法,则需要添加:

$scope.user = UserService.getUser();

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

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