javascript - How to access angularjs sessionStorage value to entire project? - Stack Overflow

I need login values for the entire project.so I'm using rootScope, but if my page is refreshed, th

I need login values for the entire project.so I'm using rootScope, but if my page is refreshed, the rootScope value gets destroyed. so instead of rootScope, I'm using AngularJS sessionStorage and now value sets successfully. Now I need to use sessionStorage value for the entire project.

how to use sessionStorage value wherever I want. I am looking for a positive replay.

Controller

if(response.data.status != null)
{   
    scope.User=response;
    rootScope.UserId=response.data.UserId;
     $window.sessionStorage.setItem("id",response.data.UserId);
     scope.id = $window.sessionStorage.getItem("id");
    state.go("userHome");
}

Thank you..!

I need login values for the entire project.so I'm using rootScope, but if my page is refreshed, the rootScope value gets destroyed. so instead of rootScope, I'm using AngularJS sessionStorage and now value sets successfully. Now I need to use sessionStorage value for the entire project.

how to use sessionStorage value wherever I want. I am looking for a positive replay.

Controller

if(response.data.status != null)
{   
    scope.User=response;
    rootScope.UserId=response.data.UserId;
     $window.sessionStorage.setItem("id",response.data.UserId);
     scope.id = $window.sessionStorage.getItem("id");
    state.go("userHome");
}

Thank you..!

Share Improve this question edited Mar 7, 2018 at 11:33 Mr. Noddy 1,5902 gold badges16 silver badges47 bronze badges asked Jun 16, 2017 at 11:51 sweetysweety 1271 gold badge2 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

I would create accessors on the $rootScope inside the "app.run" :

angular.module("myApp").run(['$rootScope', function($rootScope){
    var id = null;    
    $rootScope.getId = function(){
        if (!id) id = sessionStorage.getItem('id');

        return id;        
    };

    $rootScope.setId = function(userId) {
        id = userId;
        sessionStorage.setItem('id', userId);
    };

}]);
  • That way, you can keep on using your id inside views, you will just have to make a call.
  • You won't forget to check sessionStorage if the $rootScope has no value set.
  • You won't have to access sessionStorage every time you need the id (querying the session storage is way slower than getting an item on the heap)

You will simply have to use it like the following :

if(response.data.status != null)
{   
    scope.User=response;
    rootScope.setId(response.data.UserId);
    scope.id = rootScope.getId();
    state.go("userHome");
}

For accessing it into your views :

version 1.5+ :

you can simply bind it to the "controller as" provided by $rootscope :

<div>{{$root.getId()}}</div>

version 1.5- :

As far as you never have a method or property bound to a $scope named getId, the following will work thanks to scope inheritance (if you don't know about scope inheritance in angular, you really should spend some time googling it)

<div>{{getId()}}</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745647816a4638076.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信