简体   繁体   English

如何在AngularJs中存储用户数据

[英]How to store User Data in AngularJs

Can anyone please let me know how to store logged in user data into global scope of AngularJS, 谁能让我知道如何将登录的用户数据存储到AngularJS的全局范围内,

My Login services is looked like this 我的登录服务看起来像这样

App.service('Auth', function($http){
    return {
        isLogin:function(callback){
            $http.get('api/auth/checkLogin').success(function(res){
                callback(res);
            });
        },
        login:function(user, callback){
            $http.post('api/auth/login', user).success(function(res){
                callback(res);
            });
        },
        logout:function(callback){
            $http.get('api/auth/logout').success(function(res){
                callback(res);
            });
        }
    };
});

I'm using the same in my controller like this 我在控制器中这样使用

$scope.isLogin = false;
$scope.UserData = {};
$scope.login = function(user){
    Auth.login(user, function(res){
        if(res.status){
            $scope.isLogin = true;
            $scope.loginBoxShown = false;
            $scope.UserData = res.data;
            $location.path('/Dashboard');
        }
    });
};

Its working fine on first run but once I reload the page the variable $scope.isLogin and $scope.UserData are reset to their default value. 它在第一次运行时工作正常,但是一旦我重新加载页面,变量$scope.isLogin$scope.UserData将重置为其默认值。

Is there any way to store them in a global scope permanently before my $scope.logout(); 有没有办法在$scope.logout();之前将它们永久存储在全局范围内$scope.logout(); function delete them. 功能删除它们。

I normally use ngStorage for user data persistence. 我通常使用ngStorage进行用户数据持久化。 Found at https://github.com/gsklee/ngStorage https://github.com/gsklee/ngStorage中找到

An AngularJS module that makes Web Storage working in the Angular Way. 一个使Web存储以Angular方式工作的AngularJS模块。 Contains two services: $localStorage and $sessionStorage. 包含两个服务:$ localStorage和$ sessionStorage。

Hope it helps. 希望能帮助到你。

The question isn't really Angular specific. 这个问题并不是真的特定于Angular。 To persist data across page reloads you basically have two options localStorage (and it's little sister sessionStorage ) or cookies . 要在整个页面重新加载中保持数据持久性,您基本上有两个选择: localStorage (和它的小姐妹sessionStorage )或cookies

Both techniques are covered in other SO answers already. 两种技术都已包含在其他SO答案中

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

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