Angularjs sessionStorage and scope

后端 未结 2 1978
故里飘歌
故里飘歌 2021-01-31 09:53

Anyone know why my scope variables aren\'t being updated? It\'s mind boggling to me. The sessionStorage variables are fine but when I define them within scope variables.. I get

相关标签:
2条回答
  • 2021-01-31 10:24

    You need to serialize your user object to JSON before storing on sessionStorage, because it is implemented as a collection of string values.

    Try something like this

     sessionStorage.user = JSON.stringify($scope.user);
    

    and then

     $scope.user = JSON.parse(sessionStorage.user);
    
    0 讨论(0)
  • 2021-01-31 10:39

    Nowadays angularjs don't have the JSON.stringify just assign the $scope values in sessionStorage

    $sessionStorage.user = $scope.user;
    

    For Detailed documentation on $sessionStorage service

    AngularJS - $sessionStorage by Scriptwerx is a service for use in your AngularJS applications.

    npm install --save-dev angular-swx-session-storage

    0 讨论(0)
提交回复
热议问题